.Net下二進制形式的文件(圖片)的存儲與讀取詳細解析
.Net下圖片的常見存儲與讀取凡是有以下幾種:
存儲圖片:以二進制的形式存儲圖片時,要把數(shù)據(jù)庫中的字段設置為Image數(shù)據(jù)類型(SQL Server),存儲的數(shù)據(jù)是Byte[].
1.參數(shù)是圖片路徑:返回Byte[]類型:
public byte[] GetPictureData(string imagepath)
{
////根據(jù)圖片文件的路徑使用文件流打開,并保存為byte[]
FileStream fs = new FileStream(imagepath, FileMode.Open);//可以是其他重載方法
byte[] byData = new byte[fs.Length];
fs.Read(byData, 0, byData.Length);
fs.Close();
return byData;
}
2.參數(shù)類型是Image對象,返回Byte[]類型:
public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto)
{
//將Image轉換成流數(shù)據(jù),并保存為byte[]
MemoryStream mstream = new MemoryStream();
imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] byData = new Byte[mstream.Length];
mstream.Position = 0;
mstream.Read(byData, 0, byData.Length);
mstream.Close();
return byData;
}
好了,這樣通過上面的方法就可以把圖片轉換成Byte[]對象,然后就把這個對象保存到數(shù)據(jù)庫中去就實現(xiàn)了把圖片的二進制格式保存到數(shù)據(jù)庫中去了。下面我就談談如何把數(shù)據(jù)庫中的圖片讀取出來,實際上這是一個相反的過程。
讀取圖片:把相應的字段轉換成Byte[]即:Byte[] bt=(Byte[])XXXX
1.參數(shù)是Byte[]類型,返回值是Image對象:
public System.Drawing.Image ReturnPhoto(byte[] streamByte)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
return img;
}
2.參數(shù)是Byte[] 類型,沒有返回值,這是針對asp.net中把圖片從輸出到網(wǎng)頁上(Response.BinaryWrite)
public void WritePhoto(byte[] streamByte)
{
// Response.ContentType 的默認值為默認值為“text/html”
Response.ContentType = "image/GIF";
//圖片輸出的類型有: image/GIF image/JPEG
Response.BinaryWrite(streamByte);
}
補充:
針對Response.ContentType的值,除了針對圖片的類型外,還有其他的類型:
Response.ContentType = "application/msword";
Response.ContentType = "application/x-shockwave-flash";
Response.ContentType = "application/vnd.ms-excel";
另外可以針對不同的格式,用不同的輸出類型以適合不同的類型:
switch (dataread("document_type"))
{
case "doc":
Response.ContentType = "application/msword";
case "swf":
Response.ContentType = "application/x-shockwave-flash";
case "xls":
Response.ContentType = "application/vnd.ms-excel";
case "gif":
Response.ContentType = "image/gif";
case "Jpg":
Response.ContentType = "image/jpeg";
}
一些相關的東西,可以作為參考
Image image= GetImageFromClipboard();//實現(xiàn)從剪切板獲取圖像的功能
System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter
= new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); formatter.Serialize(stream, image);
FileStream fs=new FileStream("xx",FileMode.Open,FileAccess.Write);
fs.Write(stream.ToArray(),0,stream.ToArray().Length);
相關文章
asp.net實現(xiàn)上傳圖片時判斷圖片的模式GRB或CMYK的方法
這篇文章主要介紹了asp.net實現(xiàn)上傳圖片時判斷圖片的模式GRB或CMYK的方法,涉及asp.net針對圖片的讀取及屬性操作相關技巧,需要的朋友可以參考下2016-07-07
asp.net core mvc實現(xiàn)偽靜態(tài)功能
這篇文章主要為大家詳細介紹了asp.net core mvc實現(xiàn)偽靜態(tài)功能的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02
asp.net Repeater顯示父子表數(shù)據(jù),無閃爍
兩天在改項目bug,發(fā)現(xiàn)以前有人做的repeater顯示父子表結構展開和關閉子表數(shù)據(jù)時總是有閃爍,于是就試著改成無閃爍的,成功了,與大家分享.2009-12-12
.NET Corek中Git的常用命令及實戰(zhàn)演練
這篇文章將通過故事的形式從Git的歷史談起,并講述Git的強大之處。然后通過實戰(zhàn)演練教你如何在Github以及碼云上托管我們的代碼并進行代碼的版本控制2019-04-04
datagrid綁定list沒有數(shù)據(jù) 表頭不顯示的解決方法
datagrid綁定list沒有數(shù)據(jù) 表頭不顯示的問題,那是因為 綁定了null,你給list new一下就好 表頭就會有啦2013-05-05

