首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从SQL Server检索图像&设置为PictureBox图像属性

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

Stack Overflow用户
提问于 2013-03-26 17:01:21
回答 1查看 1.4K关注 0票数 1

我想使用实体框架从SQL Server(数据库)中检索图像,并将其设置为DisplayImage属性。我已经创建了一个用户控件,并将其继承到PictureBox (名称: PictureBox )

代码语言:javascript
复制
    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上加载图像

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-26 17:11:19

试试这段代码

代码语言:javascript
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15632972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档