将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);
}