2013-03-26 147 views
1

我想使用實體框架從SQL Server(數據庫)檢索圖像並將其設置爲PictureBox圖像屬性 我已經制作了一個用戶控件&將它繼承到PictureBox(名稱: DisplayImage)從SQL Server中檢索圖像&設置爲PictureBox圖像屬性

public static void LoadDisplay(Guid? DisplayID, string Name, byte[] image) 
    { 
     DisplayImage objDisplayImage = new DisplayImage(); 
     DisplayList.Add(objDisplayImage); 
     objDisplayImage.Name = Name; 
     MemoryStream ms = new MemoryStream(image); 
     Image myImage = Image.FromStream(ms); 
     objDisplayImage.Image = myImage; 
     objDisplayImage.DisplayID = DisplayID; 
     PlayerForm.Instance.Controls.Add(objDisplayImage); 
    } 

,但圖像沒有加載上的PictureBox

+0

什麼是形象喲格式你在用嗎? (PNG,JPEG,BMP等)。如果是BMP,請注意內存中格式與磁盤格式不同(例如,它沒有標題)。 – Dai 2013-03-26 09:07:54

+0

圖片格式爲PNG – 2013-03-26 09:10:28

+0

此外,請將您的代碼封裝在try/catch中,以查看是否有未捕獲的異常。 VS的調試器可視化器報告關於您正在創建的映像的內容? – Dai 2013-03-26 09:10:31

回答

1

試試這個代碼

try 
{ 
    // get image from object 
    byte[] _ImageData = new byte[0]; 
    _ImageData = (byte[])_SqlRetVal; 
    System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_ImageData); 
    _Image = System.Drawing.Image.FromStream(_MemoryStream); 
} 
catch (Exception _Exception) 
{ 
    // Error occurred while trying to create image 
    // send error message to console (change below line to customize error handling) 
    Console.WriteLine(_Exception.Message); 

    return null; 
}