【发布时间】:2010-11-15 03:35:50
【问题描述】:
如何在 C# 中将 System.Windows.Media.ImageSource 转换为 System.Drawing.Bitmap?
【问题讨论】:
如何在 C# 中将 System.Windows.Media.ImageSource 转换为 System.Drawing.Bitmap?
【问题讨论】:
它较旧的 OP,但对于其他人来说仍然可以派上用场,因为它需要一些时间才能找到没有 dll 互操作或剪贴板黑客的更清洁的解决方案。
这对我有用,您可以在保存到文件或 rtf 流之前使用 pngencoder 削减图像大小
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);
}
【讨论】:
请看HOW TO USE IMAGESOURCE (NO HANDLER) IN WINFORMS AS SYSTEM.DRAWING.BITMAP (HBITMAP):
如何轻松转换 WinForms System.Drawing.Bitmap 到 WPF 你从中学到的 ImageSource 文章。今天,我将解释如何做 它相反。其实他只需要 做的是从中提取处理程序 但是,BitmapSource 这种方法 不支持,因此唯一的 我们可以做的只是复制像素 BitmapSource(或BitmapFrame)成 字节数组,然后将它们复制到 HBitmap的指针。
【讨论】: