c#读取图像保存到数据库中(数据库保存图片)
程序员文章站
2024-02-25 22:01:33
复制代码 代码如下:注:mytools.g_photofield为数据库表中的图象字段名称//将图片保存到数据库中 if(th...
复制代码 代码如下:
注: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;
}