接下來(lái)簡(jiǎn)單說(shuō)一下我為什么沒(méi)有用存儲(chǔ)圖片路徑的方式,而采取了直接在MySQL中存儲(chǔ)圖片的方式。原因有兩點(diǎn):
1、本身不需要大量圖片,一個(gè)數(shù)據(jù)庫(kù)只需要一張圖片
2、軟件結(jié)構(gòu)是要通過(guò)WebService由一個(gè)主客戶端去訪問(wèn)下面附屬的幾個(gè)客戶端,如果附屬客戶端不存儲(chǔ)圖片直接供主客戶端訪問(wèn),那么主客戶端勢(shì)必就需要一個(gè)加載圖片的功能(類似于FTP的功能)。
下面還是直接上代碼吧:
public bool MapSearchWrite(string strImagePath)
{
//將圖片轉(zhuǎn)換成緩沖流
FileStream fs = new FileStream(strImagePath, FileMode.Open, FileAccess.Read);
//獲得圖片的字節(jié)數(shù)組
byte[] byImage = new byte[fs.Length];
fs.Read(byImage, 0, byImage.Length);
fs.Close();
//數(shù)據(jù)庫(kù)連接
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("地圖檢索數(shù)據(jù)庫(kù)連接失敗");
}
//判斷數(shù)據(jù)庫(kù)內(nèi)部有無(wú)記錄
string strQueryCmd = "select PicNum from images";
MySqlCommand cmdQuery = new MySqlCommand(strQueryCmd, conn);
MySqlDataReader dataReader = cmdQuery.ExecuteReader();
//執(zhí)行操作
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;
}
//關(guān)閉連接等
cmd.Dispose();
conn.Close();
conn.Dispose();
if (affectedRows <= 0)
{
return false;
}
else
{
return true;
}
}
這是把圖片插入到數(shù)據(jù)庫(kù)的操作代碼,路徑的話就是你所需要存儲(chǔ)的圖片所在的路徑(包括圖片的名字和后綴名哦),另外我這里采用的是ADO.NET的連接方式,語(yǔ)言是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("地圖檢索數(shù)據(jù)庫(kù)連接失敗");
}
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);
//將圖片字節(jié)數(shù)組加載入到緩沖流
// MemoryStream imageStream = new MemoryStream(imageByte);
//從緩沖流生成圖片
//imageResulet = Image.FromStream(imageStream, true);
}
dataReader.Dispose();
cmd.Dispose();
conn.Close();
conn.Dispose();
}
當(dāng)然這里我是照顧到Image對(duì)象不能通過(guò)WebService傳輸而把BLOB數(shù)據(jù)只轉(zhuǎn)換成byte[]在傳輸,如果不需要這個(gè)功能的換可以直接把相關(guān)代碼踢出來(lái)再將byte[]轉(zhuǎn)成圖片對(duì)象即可,一下提供兩種方法
第一種:imageByte是調(diào)用上面函數(shù)得到的byte[]的數(shù)據(jù)
//將圖片字節(jié)數(shù)組加載入到緩沖流
MemoryStream imageStream = new MemoryStream(imageByte);
//從緩沖流生成圖片
imageResulet = Image.FromStream(imageStream, true);
//pictureBox是一個(gè)顯示圖片或者視頻的C#控件
pictureBox.Image = imageResulet;
這樣就把圖片讀取出來(lái)并顯示出來(lái)了
第二種:BitMap是System.Drawingm命名空間中的
Bitmap bm = new Bitmap(new MemoryStream(imageByte));
pictureBox1.Image = bm;
那么,到此我就說(shuō)完了,當(dāng)然不是迫不得已不要把圖片存到數(shù)據(jù)庫(kù)中,可以做個(gè)url映射,返回文件流(這個(gè)目前沒(méi)試過(guò),有時(shí)間試過(guò)后再把經(jīng)驗(yàn)分享給大家)。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com