首页 > 代码库 > C#中 byte[] Image Bitmap之间的相互转化

C#中 byte[] Image Bitmap之间的相互转化

/// <summary>
        ///将图片Image转换成Byte[]
        ///</summary>
        ///<paramname="Image">image对象</param>
        ///<paramname="imageFormat">后缀名</param>
        ///<returns></returns>
        publicstatic byte[] ImageToBytes(Image Image,System.Drawing.Imaging.ImageFormat imageFormat)
        {

            if(Image == null) { return null; }

            byte[]data = http://www.cnblogs.com/peasana/archive/2012/02/13/null;

            using(MemoryStream ms= new MemoryStream())
            {

                 using(Bitmap Bitmap = new Bitmap(Image))
                {

                    Bitmap.Save(ms,imageFormat);

                    ms.Position= 0;

                    data= http://www.cnblogs.com/peasana/archive/2012/02/13/newbyte[ms.Length];

                    ms.Read(data,0, Convert.ToInt32(ms.Length));

                    ms.Flush();

                }

            }

            returndata;

        }

 

 

            ///<summary>
            ///byte[]转换成Image
            ///</summary>
            ///<paramname="byteArrayIn">二进制图片流</param>
            ///<returns>Image</returns>
            publicstatic System.Drawing.Image byteArrayToImage(byte[]byteArrayIn)
            {
                if(byteArrayIn == null)
                    returnnull;
                using(System.IO.MemoryStream ms = newSystem.IO.MemoryStream(byteArrayIn))
                {
                    System.Drawing.ImagereturnImage = System.Drawing.Image.FromStream(ms);
                    ms.Flush();
                    returnreturnImage;
                }
            }

 

   //Image转换Bitmap

   1. Bitmap img= new Bitmap(imgSelect.Image);

   2. Bitmap bmp= (Bitmap)pictureBox1.Image;

 

//Bitmap转换成Image

using System.IO;

private static System.Windows.Controls.ImageBitmap2Image(System.Drawing.Bitmap Bi)
        {           
            MemoryStreamms = new MemoryStream();
            Bi.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
            BitmapImagebImage = new BitmapImage();
            bImage.BeginInit();
            bImage.StreamSource= new MemoryStream(ms.ToArray());
            bImage.EndInit();
            ms.Dispose();
            Bi.Dispose();
            System.Windows.Controls.Imagei = new System.Windows.Controls.Image();
            i.Source= bImage;
            returni ;
        }

 

//byte[] 转换 Bitmap
 public static Bitmap BytesToBitmap(byte[]Bytes) 
        { 
            MemoryStreamstream = null; 
            try 
            { 
                stream= new MemoryStream(Bytes); 
                returnnew Bitmap((Image)new Bitmap(stream)); 
            } 
            catch(ArgumentNullException ex) 
            { 
                throwex; 
            } 
            catch(ArgumentException ex) 
            { 
                throwex; 
            } 
            finally 
            { 
                stream.Close(); 
            } 
        }  
 
//Bitmap转byte[]  
        publicstatic byte[] BitmapToBytes(BitmapBitmap) 
        { 
            MemoryStreamms = null; 
            try 
            { 
                ms= new MemoryStream(); 
                Bitmap.Save(ms,Bitmap.RawFormat); 
                byte[]byteImage = new Byte[ms.Length]; 
                byteImage= ms.ToArray(); 
                returnbyteImage; 
            } 
            catch(ArgumentNullException ex) 
            { 
                throwex; 
            } 
            finally 
            { 
                ms.Close(); 
            } 
        } 
    }