微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

  wpf/silverlight 为我们提供 System.Windows.Clipboard 实体让我们能很好的操作剪切板中的内容,下面通过一个实例程序把元件转换成图片后放入到系统剪切板。

     rendertargetBitmap renderBitmap = new rendertargetBitmap((int)(【元件】.Width),(int)(【元件】.Height),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();

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐