国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Net下二進(jìn)制形式的文件(圖片)的存儲與讀取

Net下二進(jìn)制形式的文件(圖片)的存儲與讀取<很好用>

.net 2008-06-20 14:16:27 閱讀203 評論4   字號: 訂閱

Net下二進(jìn)制形式的文件(圖片)的存儲與讀取
2008-05-11 12:19
.Net下圖片的常見存儲與讀取凡是有以下幾種:
存儲圖片:以二進(jìn)制的形式存儲圖片時(shí),要把數(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ù)庫中去就實(shí)現(xiàn)了把圖片的二進(jìn)制格式保存到數(shù)據(jù)庫中去了。下面我就談?wù)勅绾伟褦?shù)據(jù)庫中的圖片讀取出來,實(shí)際上這是一個相反的過程。
讀取圖片:把相應(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 的默認(rèn)值為默認(rèn)值為“text/html”
             Response.ContentType = "image/GIF";
            
//圖片輸出的類型有: image/GIF   image/JPEG
             Response.BinaryWrite(streamByte);
         }
補(bǔ)充:
針對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";
             }
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Response.ContentType 所有類型例舉
Response.ContentType = "image/jpg"導(dǎo)致的無法顯示 XML 頁
圖片轉(zhuǎn)化為二進(jìn)制
Response.ContentType的詳細(xì)列表
WebAPI在MVC4下的調(diào)整(4)
JSP頁面實(shí)現(xiàn)圖片、PDF字節(jié)流的顯示,Word、Excel、Zip字節(jié)流的下載功能的實(shí)現(xiàn)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服