C# 將字節流轉換為圖片的實例方法(轉)


代碼如下:

usingSystem; 
usingSystem.Collections.Generic; 
usingSystem.Linq; 
usingSystem.Text; 
usingSystem.Drawing; 
usingSystem.IO; 
namespaceMicrosoft.Form.Base 
{ 
    classImageToByte 
    { 
        /// <summary> 
        /// 圖片轉換成字節流 
        /// </summary> 
        /// <param name="img">要轉換的Image對象</param> 
        /// <returns>轉換后返回的字節流</returns> 
        publicstaticbyte[] ImgToByt(Image img) 
        { 
            MemoryStream ms = newMemoryStream(); 
            byte[] imagedata = null; 
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 
            imagedata = ms.GetBuffer(); 
            returnimagedata;             
        } 
        /// <summary> 
        /// 字節流轉換成圖片 
        /// </summary> 
        /// <param name="byt">要轉換的字節流</param> 
        /// <returns>轉換得到的Image對象</returns> 
        publicstaticImage BytToImg(byte[] byt) 
        { 
            MemoryStream ms = newMemoryStream(byt); 
            Image img = Image.FromStream(ms); 
            returnimg; 
        } 
        // 
        /// <summary> 
        /// 根據圖片路徑返回圖片的字節流byte[] 
        /// </summary> 
        /// <param name="imagePath">圖片路徑</param> 
        /// <returns>返回的字節流</returns> 
        privatestaticbyte[] getImageByte(stringimagePath) 
        { 
            FileStream files = newFileStream(imagePath, FileMode.Open); 
            byte[] imgByte = newbyte[files.Length]; 
            files.Read(imgByte, 0, imgByte.Length); 
            files.Close(); 
            returnimgByte; 
        } 
    } 
}

轉:http://www.jb51.net/article/34605.htm


免責聲明!

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



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