首頁  >  文章  >  資料庫  >  mysql如何儲存讀取圖片

mysql如何儲存讀取圖片

藏色散人
藏色散人原創
2020-11-03 10:07:233153瀏覽

mysql儲存讀取圖片的方法:首先將圖片轉換成緩衝流;然後取得圖片的位元組陣列並執行相關操作;最後透過「public void MapSearchQuery(out byte[] imageByteResulet){.. .}”讀取圖片即可。

mysql如何儲存讀取圖片

推薦:《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如何儲存讀取圖片的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn