C# 将字节流转换为图片的实例方法

        ///  
        /// 图片转换成字节流 
        ///  
        /// 要转换的Image对象 
        /// 转换后返回的字节流 
        publicstaticbyte[] ImgToByt(Image img) 
        { 
            MemoryStream ms = newMemoryStream(); 
            byte[] imagedata = null; 
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 
            imagedata = ms.GetBuffer(); 
            returnimagedata;             
        } 


        ///  
        /// 字节流转换成图片 
        ///  
        /// 要转换的字节流 
        /// 转换得到的Image对象 
        publicstaticImage BytToImg(byte[] byt) 
        { 
            MemoryStream ms = newMemoryStream(byt); 
            Image img = Image.FromStream(ms); 
            returnimg; 
        } 


        // 
        ///  
        /// 根据图片路径返回图片的字节流byte[] 
        ///  
        /// 图片路径 
        /// 返回的字节流 
        privatestaticbyte[] getImageByte(stringimagePath) 
        { 
            FileStream files = newFileStream(imagePath, FileMode.Open); 
            byte[] imgByte = newbyte[files.Length]; 
            files.Read(imgByte, 0, imgByte.Length); 
            files.Close(); 
            returnimgByte; 
        } 

 

你可能感兴趣的:(C#,C#,字节流转图片)