zoukankan      html  css  js  c++  java
  • 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 = 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/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();            }        }    }

  • 相关阅读:
    Git中清除远程仓库HTTPS认证信息的方法
    JDK8新增时间类型用在JPA中的问题
    5 个关于 API 中日期和时间设计规则
    时间标准基础知识UTC和ISO8601
    JDK8中的时间API
    2019第7周日
    顶级思维模式:推导事物的第一性原理
    JS的jsoneditor,用来操作Json格式的界面;json-editor用来根据json数据生成界面
    Java读写文件,中文乱码解决
    intellij idea 热部署、热加载设置方法
  • 原文地址:https://www.cnblogs.com/bile/p/2827389.html
Copyright © 2011-2022 走看看