准备从文件夹中读取一张图片并载入,一开始使用代码:
FileStream fs = new System.IO.FileStream("snapshot.jpg",System.IO.FileMode.Open,System.IO.FileAccess.Read);
this.CopyImageElement = Image,FromFile("snapshot.jpg");
fs.Close();
结果在 Image.FromFile("snapshot.jpg");语句
提示错误:
无法将类型“System.Drawing.Image”隐式转换为“System.Windows.Controls.Image”
遂修改为:
System.Drawing.Image img = System.Drawing.Image.FromFile("snapshot.jpg");
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(img);
IntPtr hBitmap = bmp.GetHbitmap();
System.Windows.Media.ImageSource WpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
this.CopyImageElement.Source = WpfBitmap;