将System.Windows.Media.ImageSource转换为System.Drawing.Bitmap

时间:2009-07-29 16:35:53

标签: c# wpf image

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

2 个答案:

答案 0 :(得分:12)

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

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

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

答案 1 :(得分:0)

请参阅HOW TO USE IMAGESOURCE (NO HANDLER) IN WINFORMS AS SYSTEM.DRAWING.BITMAP (HBITMAP)

  

如何轻松转换WinForms   System.Drawing.Bitmap到WPF中   你从中学到了ImageSource   文章。今天,我会解释怎么做   相反。实际上,他只需要   就是从中提取处理程序   然而,BitmapSource就是这样的方法   不受支持,因此是唯一的   我们能做的只是复制像素   BitmapSource(或BitmapFrame)成   字节数组然后将它们复制到   HBitmap的指针。

相关问题