共計 3262 個字符,預計需要花費 9 分鐘才能閱讀完成。
自動寫代碼機器人,免費開通
這篇文章主要介紹了 mysql 數據庫怎樣儲存讀取圖片,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓丸趣 TV 小編帶著大家一起了解一下。
mysql 儲存讀取圖片的方法:首先將圖片轉換成緩沖流;然后獲得圖片的字節數組并執行相關操作;最后通過“public void MapSearchQuery(out byte[] imageByteResulet){…}”讀取圖片即可。
首先,介紹一下 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
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;
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
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;
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 映射,返回文件流。
感謝你能夠認真閱讀完這篇文章,希望丸趣 TV 小編分享 mysql 數據庫怎樣儲存讀取圖片內容對大家有幫助,同時也希望大家多多支持丸趣 TV,關注丸趣 TV 行業資訊頻道,遇到問題就找丸趣 TV,詳細的解決方法等著你來學習!
向 AI 問一下細節
丸趣 TV 網 – 提供最優質的資源集合!