2011-08-25 50 views
2

我需要讀取一個jpg文件,latyer將它顯示在Image控件中。 以下作品完美:WPF - 使用JpegBitmapDecoder將文件轉換爲Byte []到BitmapSource

imgTwo.Source = FetchImage(@"C:\Image075.jpg"); 

public BitmapSource FetchImage(string URLlink) 
{ 
     JpegBitmapDecoder decoder = null; 
     BitmapSource bitmapSource = null; 
     decoder = new JpegBitmapDecoder(new Uri(URLlink, UriKind.Absolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); 
     bitmapSource = decoder.Frames[0]; 
     bitmapSource.Freeze(); 
     return bitmapSource; 
} 

我的問題是,我需要保持這種形象在數據庫中以字節](VARBINARY(MAX),並從那裏讀它,而不是直接從文件上做。 所以我需要要麼有一個byte []作爲輸入這個功能,而不是URLlink串,或保存的BitmapSource作爲字節[]。我該怎麼辦呢?

回答

4

JpegBitmapDecodersecond constructor接受一個Stream 。只需傳入一個MemoryStream,其中包含您的byte[]

using(var stream = new MemoryStream(yourByteArray)) 
{ 
    decoder = new JpegBitmapDecoder(stream, 
            BitmapCreateOptions.PreservePixelFormat, 
            BitmapCacheOption.OnLoad); 
} 
+0

Det工作繁忙。非常感謝您的幫助。 :-) – Keren