Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从SQL Server中检索图像&;设置为PictureBox图像属性_C#_Entity Framework_User Controls_Picturebox - Fatal编程技术网

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

C# 从SQL Server中检索图像&;设置为PictureBox图像属性,c#,entity-framework,user-controls,picturebox,C#,Entity Framework,User Controls,Picturebox,我想使用实体框架从SQL Server(数据库)检索图像,并将其设置为PictureBox图像属性 我制作了一个用户控件并将其继承到PictureBox(名称:DisplayImage) 但图像未加载到PictureBox上请尝试此代码 try { // get image from object byte[] _ImageData = new byte[0]; _ImageData = (byte[])_SqlRetVal; System.IO.MemorySt

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

但图像未加载到PictureBox上

请尝试此代码

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;
}
试试这个代码

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;
}

您使用的图像格式是什么?(PNG、JPEG、BMP等?)。如果是BMP,请注意内存中的格式与磁盘上的格式不同(例如,它缺少标题)。此外,请将代码包装在
try/catch
块中,以查看是否存在任何未捕获的异常。VS的debugger visualizer对您正在创建的图像报告了什么?此代码中没有例外您正在使用的图像的格式是什么?(PNG、JPEG、BMP等?)。如果是BMP,请注意内存中的格式与磁盘上的格式不同(例如,它缺少标题)。此外,请将代码包装在
try/catch
块中,以查看是否存在任何未捕获的异常。VS的debugger visualizer对您正在创建的映像报告了什么?此代码中没有异常