Skip to main content
 首页 » 编程设计

c#之如何在 WPF 的图像控件中分配 System.Drawing.Image 对象

2024年02月13日17zlslch

我正在使用一个返回 System.Drawing.Image 对象的 DLL。我在 winform c# 中使用那个 DLL,并且工作正常。现在我在 WPF 中升级我的应用程序,我很困惑如何将这个返回对象 (System.Drawing.Image) 分配给我在 wpf 中的 Image 控件?有帮助吗?

请您参考如下方法:

怎么样

// Winforms Image we want to get the WPF Image from... 
 
System.Drawing.Image imgWinForms = WindowsImageFromDLL(); 
 
// ImageSource ... 
 
BitmapImage bi = new BitmapImage(); 
 
bi.BeginInit(); 
 
MemoryStream ms = new MemoryStream(); 
 
// Save to a memory stream... 
 
imgWinForms.Save(ms, ImageFormat.Bmp); 
 
// Rewind the stream... 
 
ms.Seek(0, SeekOrigin.Begin); 
 
// Tell the WPF image to use this stream... 
 
bi.StreamSource = ms; 
 
bi.EndInit(); 

这样你的图像就不会作为文件存储在系统中。