C# 从SQL Server中检索图像&;设置为PictureBox图像属性
我想使用实体框架从SQL Server(数据库)检索图像,并将其设置为PictureBox图像属性 我制作了一个用户控件并将其继承到PictureBox(名称:DisplayImage) 但图像未加载到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
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对您正在创建的映像报告了什么?此代码中没有异常