2011-12-06 77 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

相關問題