推薦:《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映射,返回文件流(這個目前沒試過,有時間試過后再把經驗分享給大家)。
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com