从SQL Server&中检索图像设置为PictureBox图像属性

时间:2013-03-26 09:01:21

标签: c# entity-framework user-controls picturebox

我想使用Entity Framework从SQL Server(数据库)中检索图像并将其设置为PictureBox Image Property 我已经制作了一个用户控件&继承它到PictureBox(名称:DisplayImage)

    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上加载

1 个答案:

答案 0 :(得分: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;
}