我正在拼命地尝试将一张图片保存到SQL数据库中,并在我的WP上加载它。所有在线指南都说要将图像转换为字节数组,存储它,然后再将其加载回图像。
到目前为止,我已经能够使用以下方法将图像保存到字节数组中:
public static byte[] ConvertToBytes(Stream photoStream)
{
byte[] a = new Byte[photoStream.Length];
for (int i = 0; i < photoStream.Length; i++)
{
a[i] = (Byte)photoStream.ReadByte();
}
return (a);
}
这会生成一个字节数组,大小与我保存的图像类似。
建议加载图像的方法是:
1 public static BitmapImage ConvertToImage(Byte[] inputBytes)
2 {
3 MemoryStream stream = new MemoryStream(inputBytes);
4 BitmapImage image = new BitmapImage();
5 image.SetSource(stream);
6 return (image);
7 }
这个不起作用。
我在第5行遇到了这个错误:"未指定的错误"。
有没有人有什么想法来解决这个问题或者提供替代的方法/代码?
我知道有很多信息可以在线获取 - 我向你保证我已经长时间搜索并尝试了各种方法,但都无法解决问题。
非常感谢任何帮助!