当前位置 : 主页 > 数据库 > mysql >

mysql如何储存读取图片

来源:互联网 收集:自由互联 发布时间:2021-08-19
mysql储存读取图片的方法:首先将图片转换成缓冲流;然后获得图片的字节数组并执行相关操作;最后通过“public void MapSearchQuery(out byte[] imageByteResulet){...}”读取图片即可。 推荐:《

mysql储存读取图片的方法:首先将图片转换成缓冲流;然后获得图片的字节数组并执行相关操作;最后通过“public void MapSearchQuery(out byte[] imageByteResulet){...}”读取图片即可。

推荐:《mysql视频教程》

首先,介绍一下mysql相关的数据类型:MySQL中有四种BLOB类型,TinyBlob(最大255Byte), Blob(最大65K), MediunBlob(16M), LongBlob(最大4G)。这里注意一下如果你数据库出现相关的Data too long...字样可能是你选择的种类的大小不够。

接下来简单说一下我为什么没有用存储图片路径的方式,而采取了直接在MySQL中存储图片的方式。原因有两点:

1、本身不需要大量图片,一个数据库只需要一张图片

2、软件结构是要通过WebService由一个主客户端去访问下面附属的几个客户端,如果附属客户端不存储图片直接供主客户端访问,那么主客户端势必就需要一个加载图片的功能(类似于FTP的功能)。

下面还是直接上代码吧:

public bool MapSearchWrite(string strImagePath)
         {
             //将图片转换成缓冲流
             FileStream fs = new FileStream(strImagePath, FileMode.Open, FileAccess.Read);
             
             //获得图片的字节数组
             byte[] byImage = new byte[fs.Length];
             fs.Read(byImage, 0, byImage.Length);
             fs.Close();
             //数据库连接
             MySqlConnection conn = new MySqlConnection();
             conn.ConnectionString = "Server=localhost;Uid=root;Password=123456;Database=firefighting;charset=gb2312";
             try
             {
                 conn.Open();
             }
             catch
             {
                 conn.Close();
                 conn.Dispose();
                 throw new ArgumentException("地图检索数据库连接失败");
             }
             //判断数据库内部有无记录
             string strQueryCmd = "select PicNum from images";
             MySqlCommand cmdQuery = new MySqlCommand(strQueryCmd, conn);
             MySqlDataReader dataReader = cmdQuery.ExecuteReader();
             //执行操作
             MySqlCommand cmd = new MySqlCommand();
             if (dataReader.Read())
             {
                 cmd.CommandText = "update images set Image=@byImage";
             }
             else
             {
                 cmd.CommandText = "insert into images(Image) values(@byImage)";
             }
           
             cmd.CommandType = CommandType.Text;
             cmd.Parameters.Add("@byImage", MySqlDbType.MediumBlob);
             cmd.Parameters[0].Value = byImage;
             cmd.Connection = conn;
          
             int affectedRows = 0;
             try
             {
                 affectedRows = cmd.ExecuteNonQuery();
             }
             catch
             {
                 affectedRows = -1;
             }
             //关闭连接等
             cmd.Dispose();
             conn.Close();
             conn.Dispose();
             if (affectedRows <= 0)
             {
                 return false;
             }
             else
             {
                 return true;
             }
         }

这是把图片插入到数据库的操作代码,路径的话就是你所需要存储的图片所在的路径(包括图片的名字和后缀名哦),另外我这里采用的是ADO.NET的连接方式,语言是C#的,其他代码也不用我解释了......

下面是读取MySQL中的图片的代码

 public void MapSearchQuery(out byte[] imageByteResulet)
         {
             imageByteResulet = null;
             MySqlConnection conn = new MySqlConnection();
             conn.ConnectionString = "Server=localhost;Uid=root;Password=123456;Database=firefighting;charset=gb2312";
             try
             {
                 conn.Open();
             }
             catch
             {
                 conn.Close();
                 conn.Dispose();
                 throw new ArgumentException("地图检索数据库连接失败");
             }
             string strQueryCmd = "select Image from images limit 1";
             MySqlCommand cmd = new MySqlCommand(strQueryCmd, conn);
             MySqlDataReader dataReader = null;
             try
             {
                 dataReader = cmd.ExecuteReader();
             }
             catch
             {
                 dataReader.Dispose();
                 cmd.Dispose();
                 conn.Close();
                 conn.Dispose();
                 throw new ArgumentException("地图检索查询地图失败");
             }
             if (dataReader.Read())
             {
                 imageByteResulet = new byte[dataReader.GetBytes(0, 0, null, 0, int.MaxValue)];
                 dataReader.GetBytes(0, 0, imageByteResulet, 0, imageByteResulet.Length);
                 //将图片字节数组加载入到缓冲流
                 // MemoryStream imageStream = new MemoryStream(imageByte);
                 //从缓冲流生成图片
                 //imageResulet = Image.FromStream(imageStream, true);
             }
             dataReader.Dispose();
             cmd.Dispose();
             conn.Close();
             conn.Dispose();
         }

当然这里我是照顾到Image对象不能通过WebService传输而把BLOB数据只转换成byte[]在传输,如果不需要这个功能的换可以直接把相关代码踢出来再将byte[]转成图片对象即可,一下提供两种方法

第一种:imageByte是调用上面函数得到的byte[]的数据

//将图片字节数组加载入到缓冲流  
MemoryStream imageStream = new MemoryStream(imageByte);
                //从缓冲流生成图片                
                imageResulet = Image.FromStream(imageStream, true);
                //pictureBox是一个显示图片或者视频的C#控件
                pictureBox.Image = imageResulet;

这样就把图片读取出来并显示出来了

第二种:BitMap是System.Drawingm命名空间中的

Bitmap bm = new Bitmap(new MemoryStream(
imageByte
));
            
    
 pictureBox1.Image = bm;

那么,到此我就说完了,当然不是迫不得已不要把图片存到数据库中,可以做个url映射,返回文件流(这个目前没试过,有时间试过后再把经验分享给大家)。

以上就是mysql如何储存读取图片的详细内容,更多请关注自由互联其它相关文章!

网友评论