我有一个SQL Server数据库,其中存储了PNG图片。这些截图的值以十六进制表示(0x085A3B...)。如何从“截图”(我的自定义数据类型)转换为“Image”,或类似于“BitmapImage”的东西?
首先,我像这样获取截图:
上述方法会返回一个类似于字节数组的东西。
首先,我像这样获取截图:
private Screenshot LoadScreenshot()
{
using (var context = new Context())
{
return context.Screenshots.FirstOrDefault();
}
}
上述方法会返回一个类似于字节数组的东西。
byte[40864]
我无法执行以下操作,因为我遇到了异常(我不知道是哪种异常以及为什么会出现):
public BitmapImage ImageFromBuffer(Byte[] bytes)
{
MemoryStream stream = new MemoryStream(bytes);
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = stream;
image.EndInit(); //the compiler breaks here
return image;
}
我正在使用C#和WPF
谢谢
编辑:
这是我的异常:
System.Runtime.Serialization.SafeSerializationManager 找不到适合完成此操作的成像组件
如何解决:
我需要添加这行代码:
Byte[] screenshotBytes = screenshot.Screenshot; //.Screenshot is a byte [] (I dont knwo why it didnt work before)
而@frebinfrancis的方法