如何在WP7上的SQL数据库中存储图像
本文关键字:数据库 存储 图像 SQL 上的 WP7 | 更新日期: 2023-09-27 18:16:53
我拼命地试图将图像保存到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行):"不明错误"
有没有人知道如何解决这个问题,或者可以建议一个替代方法/代码?
我知道网上有信息——我可以向你保证,我已经为一种工作方法搜索了很长时间,但却一无所获。
任何帮助将非常感激!
我使用:
public static byte[] ConvertToBytes(String imageLocation)
{
StreamResourceInfo sri = Application.GetResourceStream(new Uri(imageLocation, UriKind.RelativeOrAbsolute));
BinaryReader binary = new BinaryReader(sri.Stream);
byte[] imgByteArray = binary.ReadBytes((int)(sri.Stream.Length));
binary.Close();
binary.Dispose();
return imgByteArray;
}
public static WriteableBitmap ConvertToImage(Byte[] inputBytes)
{
MemoryStream ms = new MemoryStream(inputBytes);
WriteableBitmap img = new WriteableBitmap(400, 400);
img.LoadJpeg(ms);
return (img);
}
谢谢大家的帮助。
我用了那个代码
byte[] Arr = Convert.FromBase64String(Im); >> i think you need that steep
Stream memStream = new MemoryStream(Arr);
WriteableBitmap wbimg = PictureDecoder.DecodeJpeg(memStream);
image2.Source = wbimg;
image2.Tag = IlbumID;
如果NAT工作
试
memStream.Read (Arr ,0, Arr.Length );