在我的wpf mvvm應用程序中,我編寫了一個圖像上傳代碼並保存到數據庫。 代碼工作正常,圖像保存到數據庫。 在這裏,我需要從數據庫中檢索圖像,並顯示在圖像box.Here是我的插入代碼從sql數據庫檢索圖像(字節數組)並顯示圖像
public void Upload(object obj)
{
try
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".png";
dlg.Filter = "Image files (*.png;*.jpg)|*.png;*.jpg";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
string filename = dlg.FileName;
UploadText = filename;
FileStream FS = new FileStream(filename, FileMode.Open, FileAccess.Read);
byte[] img = new byte[FS.Length];
FS.Read(img, 0, Convert.ToInt32(FS.Length));
UploadLogo = img;
Stream reader = File.OpenRead(filename);
System.Drawing.Image photo = System.Drawing.Image.FromStream((Stream)reader);
MemoryStream finalStream = new MemoryStream();
photo.Save(finalStream, ImageFormat.Png);
// translate to image source
PngBitmapDecoder decoder = new PngBitmapDecoder(finalStream, BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.Default);
ClientLogo = decoder.Frames[0]; ;
}
}
catch (Exception ex)
{
throw ex;
}
}
我怎麼能事先此字節的數據轉換爲圖像
感謝
這回答了幾個星期前http://stackoverflow.com/questions/25862980/how-to-download-and-view-images-from-sql-server-table/25863338 – Aymeric 2014-11-05 13:22:30