wpf/silverlight 將元件轉換成圖片放入剪切板

  wpf/silverlight 爲我們提供 System.Windows.Clipboard 實體讓我們能很好的操作剪切板中的內容,下面通過一個實例程序把元件轉換成圖片後放入到系統剪切板。

     RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)(【元件】.Width), (int)(【元件】.Height), 96, 96, PixelFormats.Pbgra32);


                renderBitmap.Render(【元件】);
                System.IO.MemoryStream outStream = new MemoryStream();
                PngBitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Interlace = PngInterlaceOption.Off;
                encoder.Frames.Add(BitmapFrame.Create(renderBitmap));

       //可以內存流改爲文件流,將生成Png格式的圖片文件
                encoder.Save(outStream);
                BitmapImage bitImg = new BitmapImage();
                bitImg.BeginInit();
                bitImg.StreamSource = outStream;
                bitImg.EndInit();

          //將圖片放入到剪切板
                System.Windows.Clipboard.SetImage(bitImg);

 

  附:對系統剪切板的說明

     System.Windows.Clipboard  爲我們提供了

          獲取

                System.Windows.Clipboard.GetAudioStream(); 
                System.Windows.Clipboard.GetDataObject();
                System.Windows.Clipboard.GetData();
                System.Windows.Clipboard.GetFileDropList();
                System.Windows.Clipboard.GetImage();
                System.Windows.Clipboard.GetText();

          設置

                System.Windows.Clipboard.setAudioStream(); 
                System.Windows.Clipboard.setDataObject();
                System.Windows.Clipboard.setData();
                System.Windows.Clipboard.setFileDropList();
                System.Windows.Clipboard.setImage();
                System.Windows.Clipboard.setText();

      多種格式的數據存/取方式,但並不代表這幾種數據能同時存在,在同一時刻剪切板只允許一種類型數據的存在,所以在使用時需要注意,剪切板是個公共交互空間在使用時對於特定的數據可以採用概要、自定義數據格式、或加密的方式保證數據的安全,和對自身數據的識別。

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章