2011-12-06 76 views
3

我拼命尝试将图像保存到SQL数据库,然后将其加载到我的WP上。所有在线指南都表示将图像转换为字节数组,存储它,然后将其重新加载到图像中。如何将图像存储在WP7上的SQL数据库中

到目前为止,我已经能够将图像保存到使用一个字节数组:

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行): “未指定的错误”

没有任何人有任何想法如何解决这个问题还是可以提出一个替代方法/代码?

我知道网上有信息 - 我可以向你保证,我已经搜索了很长时间,很难找到一种工作方法,并且无能为力。

任何帮助将不胜感激!

+0

请说明如何从数据库中获取inputbytes。 –

+0

试试这个链接http://www.redmondpie.com/inserting-in-and-retrieving-image-from-sql-server-database-using-c/我相信它可以帮助 – MethodMan

+0

为了测试的目的,米甚至没有使用数据库。我将图像转换为字节,然后将这些字节发送到ConvertToImage。我正在查看你的链接DJ KRAZE – Cameron

回答

2

我设法解决这个使用:

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); 
    } 

感谢您的帮助家伙。

-1

我使用的代码

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); 
+0

im是来自SQL数据库的值 – raed

+0

谢谢,但我得到“COMException未处理”...“未指定的错误”。 “memStream.Read(Arr,0,Arr.Length);”返回一个整数? – Cameron

相关问题