c#中image和byte[]的互相轉換


參數是圖片路徑:返回Byte[]類型:

    //參數是圖片的路徑
        public byte[] GetPictureData(string imagePath)
        {
            FileStream fs = new FileStream(imagePath, FileMode.Open);
            byte[] byteData = new byte[fs.Length];
            fs.Read(byteData, 0, byteData.Length);
            fs.Close();
            return byteData;
        }

參數類型是Image對象,返回Byte[]類型

        //將Image轉換成流數據,並保存為byte[] 
        public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto)
        {
            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[]類型,返回值是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;
        }

參數是Byte[] 類型,沒有返回值(ASP.NET輸出圖片)

public void WritePhoto(byte[] streamByte)
        {
            // Response.ContentType 的默認值為默認值為“text/html”
            Response.ContentType = "image/GIF";
            //圖片輸出的類型有: image/GIF     image/JPEG
            Response.BinaryWrite(streamByte);
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM