2011-08-25 47 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