.Net下二进制形式的文件(图片)的存储与读取 [ZT]
生活随笔
收集整理的這篇文章主要介紹了
.Net下二进制形式的文件(图片)的存储与读取 [ZT]
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
.Net下圖片的常見存儲與讀取凡是有以下幾種:
存儲圖片:以二進制的形式存儲圖片時,要把數據庫中的字段設置為Image數據類型(SQL Server),存儲的數據是Byte[].
1.參數是圖片路徑:返回Byte[]類型: ?public?byte[]?GetPictureData(string?imagepath)
????????{
????????????/**/////根據圖片文件的路徑使用文件流打開,并保存為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.參數類型是Image對象,返回Byte[]類型: ?public?byte[]?PhotoImageInsert(System.Drawing.Image?imgPhoto)
????????{
????????????//將Image轉換成流數據,并保存為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[]對象,然后就把這個對象保存到數據庫中去就實現了把圖片的二進制格式保存到數據庫中去了。下面我就談談如何把數據庫中的圖片讀取出來,實際上這是一個相反的過程。
讀取圖片:把相應的字段轉換成Byte[]即:Byte[] bt=(Byte[])XXXX
1.參數是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.參數是Byte[] 類型,沒有返回值,這是針對asp.net中把圖片從輸出到網頁上(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數據類型(SQL Server),存儲的數據是Byte[].
1.參數是圖片路徑:返回Byte[]類型: ?public?byte[]?GetPictureData(string?imagepath)
????????{
????????????/**/////根據圖片文件的路徑使用文件流打開,并保存為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.參數類型是Image對象,返回Byte[]類型: ?public?byte[]?PhotoImageInsert(System.Drawing.Image?imgPhoto)
????????{
????????????//將Image轉換成流數據,并保存為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[]對象,然后就把這個對象保存到數據庫中去就實現了把圖片的二進制格式保存到數據庫中去了。下面我就談談如何把數據庫中的圖片讀取出來,實際上這是一個相反的過程。
讀取圖片:把相應的字段轉換成Byte[]即:Byte[] bt=(Byte[])XXXX
1.參數是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.參數是Byte[] 類型,沒有返回值,這是針對asp.net中把圖片從輸出到網頁上(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";
????????????}
轉載于:https://www.cnblogs.com/RobotTech/archive/2008/01/28/1056138.html
總結
以上是生活随笔為你收集整理的.Net下二进制形式的文件(图片)的存储与读取 [ZT]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle结构设计技巧(访问数据库象访
- 下一篇: 希望不要T我~~哈哈...