wpf/silverlight 将元件转换成图片放入剪切板

来源:互联网 发布:软件安全性测试 试题 编辑:程序博客网 时间:2024/05/16 17:40

  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();

      多种格式的数据存/取方式,但并不代表这几种数据能同时存在,在同一时刻剪切板只允许一种类型数据的存在,所以在使用时需要注意,剪切板是个公共交互空间在使用时对于特定的数据可以采用概要、自定义数据格式、或加密的方式保证数据的安全,和对自身数据的识别。