.Net下二進位形式的檔案(圖片)的儲存與讀取 [ZT]

來源:互聯網
上載者:User
.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";
            }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.