.Net下二進制形式的文件(圖片)的存儲與讀取詳細解析
.Net下圖片的常見存儲與讀取凡是有以下幾種:
存儲圖片:以二進制的形式存儲圖片時,要把數(shù)據(jù)庫中的字段設(shè)置為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轉(zhuǎn)換成流數(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;
}
好了,這樣通過上面的方法就可以把圖片轉(zhuǎn)換成Byte[]對象,然后就把這個對象保存到數(shù)據(jù)庫中去就實現(xiàn)了把圖片的二進制格式保存到數(shù)據(jù)庫中去了。下面我就談?wù)勅绾伟褦?shù)據(jù)庫中的圖片讀取出來,實際上這是一個相反的過程。
讀取圖片:把相應(yīng)的字段轉(zhuǎn)換成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";
}
一些相關(guān)的東西,可以作為參考
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);
相關(guān)文章
asp.net Repeater控件的說明及詳細介紹及使用方法
Repeater控件是Web 服務(wù)器控件中的一個容器控件,它使您可以從頁的任何可用數(shù)據(jù)中創(chuàng)建出自定義列表。2010-04-04利用ASP.NET MVC和Bootstrap快速搭建個人博客之后臺dataTable數(shù)據(jù)列表
jQuery dataTables 插件是一個優(yōu)秀的表格插件,應(yīng)用非常廣泛,本文給大家介紹利用ASP.NET MVC和Bootstrap快速搭建個人博客之后臺dataTable數(shù)據(jù)列表,非常不錯,具有參考借鑒價值,感興趣的朋友一起看下吧2016-07-07asp.net(c#)下各種進制間的輕松轉(zhuǎn)換(2進制、8進制、10進制、16進制)
在.NET Framework中,System.Convert類中提供了較為全面的各種類型、數(shù)值之間的轉(zhuǎn)換功能。2010-10-10Asp.net使用SignalR實現(xiàn)聊天室的功能
這篇文章主要介紹了Asp.net使用SignalR實現(xiàn)聊天室的功能的相關(guān)資料,需要的朋友可以參考下2016-04-04Asp.net MVC使用swupload實現(xiàn)多圖片上傳功能
這篇文章主要為大家詳細介紹了Asp.net MVC使用swupload實現(xiàn)多圖片上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07