2013-03-26 96 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; 
}