C# · 12月 26, 2021

c#读取图像保存到数据库中(数据库保存图片)

复制代码 代码如下:
注: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,Convert.ToInt32 (ms.Length ));
m_DaTarow[MyTools.g_PhotoField] = myData;

}
catch(System.Exception ee)
{
Message@R_743_2419@.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)
{
Message@R_743_2419@.Show(ex.Message);
}
}
else
{
this.picPhoto.Image= null;
}