C# 如何在WP7上的SQL数据库中存储图像
我正在拼命地尝试将一个图像保存到SQL数据库中,然后将其加载到我的WP中。所有在线指南都要求将图像转换为字节数组,存储它,然后将其加载回图像 到目前为止,我已经能够使用以下方法将图像保存到字节数组中:C# 如何在WP7上的SQL数据库中存储图像,c#,database,image,windows-phone-7,C#,Database,Image,Windows Phone 7,我正在拼命地尝试将一个图像保存到SQL数据库中,然后将其加载到我的WP中。所有在线指南都要求将图像转换为字节数组,存储它,然后将其加载回图像 到目前为止,我已经能够使用以下方法将图像保存到字节数组中: public static byte[] ConvertToBytes(Stream photoStream) { byte[] a = new Byte[photoStream.Length]; for (int i = 0; i
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);
}
这不管用
我得到这个错误(在第5行):
“未指定的错误”
有没有人知道如何解决这个问题,或者可以建议其他方法/代码
我知道网上有一些信息,我可以向你保证,我一直在努力寻找一种工作方法,但什么都没找到
任何帮助都将不胜感激 我用了那个密码
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;
如果那没用的话
试一试
我通过以下方法解决了这个问题:
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);
}
谢谢大家的帮助。请展示您是如何从数据库中获取inputbytes的。请尝试此链接我相信它可以帮助您进行测试,我甚至不使用数据库。我将图像转换为字节,然后将这些字节发送到ConvertToImage。我正在看你的链接,DJKRAZE@DJKRAZE:此示例使用WP7中不支持的System.Drawing。@CameronFisher:那么您现在的主要问题是如何显示图像?im是SQL数据库中的值谢谢,但我得到“COMException未处理”。。。“未指定错误”。“memStream.Read(Arr,0,Arr.Length);”返回一个整数?
memStream.Read (Arr ,0, Arr.Length );
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);
}