【发布时间】:2015-02-03 08:26:06
【问题描述】:
我有一个存储 PNG 的 SQL Server 数据库。屏幕截图的值是十六进制(0x085A3B ...)。如何从“Screenshot”(我自己的数据类型)转换为“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 方法
【问题讨论】:
-
我看到了他的帖子。我的问题是我遇到了异常
-
那个问题也有例外。
-
答案对我不起作用
-
异常是运行时错误,而不是编译时问题。您可以发布您获得的异常的详细信息吗?另外,您是否尝试过将该字节数组保存到扩展名为 .png 的文件中 - 是否生成了正确的图像?