zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

c#数据库 图片 读取 保存 图像
2023-06-13 09:15:16 时间

复制代码代码如下:


注:MyTools.g_PhotoField为数据库表中的图象字段名称
//将图片保存到数据库中
    if(this.picPhoto.Image==null)
    {
     m_DataRow[MyTools.g_PhotoField]=DBNull.Value;
    }
    else
    {
     try
     {
      MemoryStreamms=newMemoryStream();
      picPhoto.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);
      byte[]myData=newByte[ms.Length];
      ms.Position=0;
      ms.Read(myData,0,Convert.ToInt32(ms.Length));
      m_DataRow[MyTools.g_PhotoField]=myData;

     }
     catch(System.Exceptionee)
     {
      MessageBox.Show(ee.Message);
     }
    }//else

//读取图象
    if(this.m_DataRow[MyTools.g_PhotoField]!=DBNull.Value)
    {
     try
     {
      Byte[]byteBLOBData= newByte[0];
      byteBLOBData=(Byte[])m_DataRow[MyTools.g_PhotoField];
      MemoryStreamstmBLOBData=newMemoryStream(byteBLOBData);
      this.picPhoto.Image=Image.FromStream(stmBLOBData);
     }
     catch(Exceptionex)
     {
      MessageBox.Show(ex.Message);
     }
    }
    else
    {
     this.picPhoto.Image=null;
    }