Byte[]、Image、Bitmap 之间的相互转换

简介: 原文:Byte[]、Image、Bitmap 之间的相互转换///         /// 将图片Image转换成Byte[]        ///         /// image对象        /// 后缀名        ///         public static byte[] ImageToBytes(Image Image, System.
原文: Byte[]、Image、Bitmap 之间的相互转换

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

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

            byte[] data = null;

            using (MemoryStream ms= new MemoryStream())
            {

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

                    Bitmap.Save(ms, imageFormat);

                    ms.Position = 0;

                    data = new byte[ms.Length];

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

                    ms.Flush();

                }

            }

            return data;

        }

 

 

            /// <summary>
            /// byte[]转换成Image
            /// </summary>
            /// <param name="byteArrayIn">二进制图片流</param>
            /// <returns>Image</returns>
            public static System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
            {
                if (byteArrayIn == null)
                    return null;
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArrayIn))
                {
                    System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
                    ms.Flush();
                    return returnImage;
                }
            }

 

    //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.Image Bitmap2Image(System.Drawing.Bitmap Bi)
        {           
            MemoryStream ms = new MemoryStream();
            Bi.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            BitmapImage bImage = new BitmapImage();
            bImage.BeginInit();
            bImage.StreamSource = new MemoryStream(ms.ToArray());
            bImage.EndInit();
            ms.Dispose();
            Bi.Dispose();
            System.Windows.Controls.Image i = new System.Windows.Controls.Image();
            i.Source = bImage;
            return i ;
        }

 

//byte[] 转换 Bitmap
 public static Bitmap BytesToBitmap(byte[] Bytes) 
        
            MemoryStream stream = null; 
            try 
            
                stream = new MemoryStream(Bytes); 
                return new Bitmap((Image)new Bitmap(stream)); 
            
            catch (ArgumentNullException ex) 
            
                throw ex; 
            
            catch (ArgumentException ex) 
            
                throw ex; 
            
            finally 
            
                stream.Close(); 
            
        }  
 
//Bitmap转byte[]  
        public static byte[] BitmapToBytes(Bitmap Bitmap) 
        
            MemoryStream ms = null; 
            try 
            
                ms = new MemoryStream(); 
                Bitmap.Save(ms, Bitmap.RawFormat); 
                byte[] byteImage = new Byte[ms.Length]; 
                byteImage = ms.ToArray(); 
                return byteImage; 
            
            catch (ArgumentNullException ex) 
            
                throw ex; 
            
            finally 
            
                ms.Close(); 
            
        
    

目录
相关文章
|
存储 C#
[uwp]ImageSource和byte[]相互转换
原文:[uwp]ImageSource和byte[]相互转换 最近做一个小app遇到一个问题,到目前还没有比较好的解决方法(可能是我查的资料不够多) 需求如下: 1.把一个Image中的图像保存到字节数组; 2.把字节数组转换为ImageSource,通过Image控件展示图像. 上面两个需求恰恰是相反的过程,为了实现这个,我倒网上找了好多,但基本都是wp7,wp8,wpf的方案,在win10上没法用。
1397 0
|
C#
【C#/WPF】Bitmap、BitmapImage、ImageSource 、byte[]转换问题
原文:【C#/WPF】Bitmap、BitmapImage、ImageSource 、byte[]转换问题 C#/WPF项目中,用到图像相关的功能时,涉及到多种图像数据类型的相互转换问题,这里做了个整理。
4417 0
|
C# 索引
C#图片灰度处理(位深度24→位深度8),用灰度数组byte[]新建一个8位灰度图像Bitmap 。
原文:C#图片灰度处理(位深度24→位深度8) #region 灰度处理 /// /// 将源图像灰度化,并转化为8位灰度图像。 /// /// 源图像。
3364 0
C# string类型和byte[]类型相互转换
string类型转成byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str ); byte[]转成string: string str = System.
1537 0
|
Java 索引
java中byte数组与十六进制字符串相互转换
<p>最近在做加密算法的研究和使用,经常会用到byte数组和十六进制字符串的转换。之前对于此类问题我一般都是使用BigInteger这个类转换一下算了,这样为了看输出不是乱码。这其实都不是根本上的解决方案。</p> <p>最简单的转换方法:</p> <p style=""></p> <pre code_snippet_id="328081" snippet_file_name="bl
2783 0