加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 04:04:25 所属栏目:百科 来源:网络整理
导读:复制代码 代码如下: 注:MyTools.g_PhotoField为数据库表中的图象字段名称 //将图片保存到数据库中 if(this.picPhoto.Image==null) { m_DataRow[MyTools.g_PhotoField]=DBNull.Value; } else { try { MemoryStream ms = new MemoryStream (); picPhoto.Image.

复制代码 代码如下:

注: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)
     {
      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;
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读