我正在从智能卡读取数据。 此数据也包含图片。 ReadData类中获取图片的代码:
public Bitmap GetPhotoFile()
{
byte[] photoFile = GetFile("photo_file");
Bitmap photo = new Bitmap(new MemoryStream(photoFile));
return photo;
}
xaml 中的代码:
imgphoto = ReadData.GetPhotoFile();
生成错误:
无法将类型“System.Drawing.Bitmap”隐式转换为“System.Windows.Controls.Image”
最好的方法是什么?
最佳答案
不要从该文件创建 System.Drawing.Bitmap
。 Bitmap
是 WinForms,不是 WPF。
相反,创建一个 WPF BitmapImage
public ImageSource GetPhotoFile()
{
var photoFile = GetFile("photo_file");
var photo = new BitmapImage();
using (var stream = new MemoryStream(photoFile))
{
photo.BeginInit();
photo.CacheOption = BitmapCacheOption.OnLoad;
photo.StreamSource = stream;
photo.EndInit();
}
return photo;
}
然后将返回的ImageSource
赋给Image控件的Source
属性:
imgphoto.Source = ReadData.GetPhotoFile();
关于wpf - 将 drawing.bitmap 转换为 windows.controls.image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41579023/