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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-19 我要评论
想了解c#读取图像保存到数据库中(数据库保存图片)的相关内容吗,在本文为您仔细讲解数据库保存图片的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:读取图像,数据库保存图片,下面大家一起来学习吧。

复制代码 代码如下:

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们