将System.Windows.Media.ImageSource转换为System.Drawing.Bitmap [英] Convert System.Windows.Media.ImageSource to System.Drawing.Bitmap

查看:416
本文介绍了将System.Windows.Media.ImageSource转换为System.Drawing.Bitmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将System.Windows.Media.ImageSource转换为C#中的System.Drawing.Bitmap?

How can I convert a System.Windows.Media.ImageSource to a System.Drawing.Bitmap in C#?

推荐答案

它的旧OP,但它仍然可以为其他人派上用场,因为它需要一些时间来找到没有dll互操作或剪贴板黑客的清洁解决方案。

its older OP, but still it can come handy for some other people, as it took some time to find cleaner solution without dll interop or clipboard hacks.

这对我有用,您可以使用pngencoder在保存到文件或rtf流之前剪切图像大小

this worked for me, you can use pngencoder to cut the image size before saving to file or rtf stream

private System.Drawing.Image ImageWpfToGDI(System.Windows.Media.ImageSource image) {
  MemoryStream ms = new MemoryStream();
  var encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();
  encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(image as System.Windows.Media.Imaging.BitmapSource));
  encoder.Save(ms);
  ms.Flush();      
  return System.Drawing.Image.FromStream(ms);
}

这篇关于将System.Windows.Media.ImageSource转换为System.Drawing.Bitmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆