永发信息网

WPF中显示System.Drawing.Image对象,怎么办?急急急

答案:2  悬赏:60  手机版
解决时间 2021-04-04 03:04
  • 提问者网友:骨子里的高雅
  • 2021-04-03 16:37
WPF中显示System.Drawing.Image对象,怎么办?急急急
最佳答案
  • 五星知识达人网友:不想翻身的咸鱼
  • 2021-04-03 16:42




一种方法:
System.Drawing.Image bmp=...; // 自己初始化的有效的 image
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);// 格式自处理,这里用 bitmap
// 下行,初始一个 ImageSource 作为 myImage 的Source
System.Windows.Media.Imaging.BitmapImage bi=new System.Windows.Media.Imaging.BitmapImage();
bi.BeginInit();
bi.StreamSource = new MemoryStream(ms.ToArray()); // 不要直接使用 ms
bi.EndInit();
myImage.Source = bi; // done!
ms.Close();

另一个方法(更加常用的方法):
System.Drawing.Image bmp=...; // 自己初始化的有效的 image
System.Windows.Media.Imaging.BitmapSource bi =
System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bmp.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
// 上面以 bmp 格式为例的,其他格式自处理
myImage.Source = bi; // Done!

参考一下System.Windows.Interop.Imaging 的以下方法:
CreateBitmapSourceFromHBitmap() // 从 HBITMAP 得到 ImageSource
CreateBitmapSourceFromHIcon() // 从 HICON 得到 ImageSource
CreateBitmapSourceFromMemorySection() // 从 HMEM 得到 ImageSource

Good luck
全部回答
  • 1楼网友:街头电车
  • 2021-04-03 17:25
使用数据流
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯