Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将System.Windows.Media.ImageSource转换为System.Drawing.Bitmap_C#_Wpf_Image - Fatal编程技术网

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

C# 将System.Windows.Media.ImageSource转换为System.Drawing.Bitmap,c#,wpf,image,C#,Wpf,Image,如何将System.Windows.Media.ImageSource转换为C#中的System.Drawing.Bitmap?请参阅: 如何轻松转换WinForms System.Drawing.Bitmap到WPF中 ImageSource您从中了解到了什么 文章今天,我将解释如何做 事实恰恰相反。事实上,他要做的就是 要做的是从中提取处理程序 然而,BitmapSource不支持这种方法 不受支持,因此是唯一的 我们所能做的就是复制图像的像素 BitmapSource(或BitmapFra

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

请参阅:

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


它的操作比较老,但对其他人来说仍然很方便,因为它花了一些时间来寻找没有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);
}

如果需要使用alpha通道(带有透明部分)转换图像,请使用System.Windows.Media.Imaging.PngBitmapEncoder()而不是System.Windows.Media.Imaging.BmpBitmapEncoder()。@DariuszWasacz我无法获得此答案。它抱怨image.Source不工作:“System.Drawing.image”不包含“Source”的定义,并且找不到接受“System.Drawing.image”类型的第一个参数的扩展方法“Source”。我检查了MSDN,图像没有源属性。你知道需要修复什么吗?@kayleeFrye_onDeck:请注意,System.Drawing和System.Windows.Media命名空间都包含类“Image”。看起来您在参数“System.Windows.Media.Image”和“System.Drawing.Image”中省略了命名空间,这是从使用语句中获取的。请检查您的代码是否与上述代码完全相同。我觉得我在这里遗漏了什么,@DariuszWasacz。你说得对,它使用的是System.Drawing.Image,这很好,但我找不到System.Windows.Media中的“Image”成员。我能找到的最接近的是System.Windows.Media.ImageSource。因为上面的代码使用了Image的.Source属性,所以看起来像是逻辑上的飞跃。@kayleeFrye_onDeck:上面的代码最初不是我的:-)。我的代码是这样的:public static System.Drawing.Image ImageWpfToGdi2(此ImageSource映像){var ms=new MemoryStream();var encoder=new System.Windows.Media.Imaging.pngbitmapcoder();encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create)((System.Windows.Media.Imaging.BitmapSource)image));encoder.Save(ms);ms.Flush();var btm=(System.Drawing.Bitmap)System.Drawing.image.FromStream(ms);return btm;}这篇文章和以前的文章都是相同的方法:将位图扫描行复制到字节数组,然后从内存块创建位图。位图假定内存块在位图生存期内包含扫描线。但是,在保留固定(byte*pB=bits)代码后,.net内存管理器会覆盖内存,位图会损坏。解决方案是使用Marshal.AllocHGlobal,但在这种情况下,内存块必须由用户代码维护(即:在Marshal.FreeHGlobal使用后必须释放);这就是为什么我们要求人们发布答案而不是链接。