c#读取图像保存到数据库中(数据库保存图片)
更新时间:2020年6月25日 11:38 点击:1646
复制代码 代码如下:
注:MyTools.g_PhotoField为数据库表中的图象字段名称
//将图片保存到数据库中
if(this.picPhoto.Image==null)
{
m_DataRow[MyTools.g_PhotoField]=DBNull.Value;
}
else
{
try
{
MemoryStream ms = new MemoryStream ();
picPhoto.Image.Save (ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte [] myData = new Byte [ms.Length ];
ms.Position = 0;
ms.Read (myData,0,Convert.ToInt32 (ms.Length ));
m_DataRow[MyTools.g_PhotoField] = myData;
}
catch(System.Exception ee)
{
MessageBox.Show(ee.Message);
}
}//else
//读取图象
if(this.m_DataRow[MyTools.g_PhotoField]!=DBNull.Value)
{
try
{
Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])m_DataRow[MyTools.g_PhotoField];
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
this.picPhoto.Image= Image.FromStream(stmBLOBData);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
this.picPhoto.Image= null;
}
上一篇: C#判断数据类型的简单示例代码
相关文章
VS2010+Opencv+MFC读取图像和视频显示在Picture控件
这篇文章主要为大家详细介绍了VS2010+Opencv+MFC读取图像和视频显示在Picture控件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25- 这篇文章主要介绍了使用c#读取图像保存到数据库中的方法,大家参考使用吧...2020-06-25