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

https://blog.csdn.net/hcf_force/article/details/21173343

[csharp]  view plain  copy
  1. /// <summary>  
  2.         /// 将图片Image转换成Byte[]  
  3.         /// </summary>  
  4.         /// <param name="Image">image对象</param>  
  5.         /// <param name="imageFormat">后缀名</param>  
  6.         /// <returns></returns>  
  7.         public static byte[] ImageToBytes(Image Image, System.Drawing.Imaging.ImageFormat imageFormat)  
  8.         {  
  9.   
  10.             if (Image == null) { return null; }  
  11.   
  12.             byte[] data = http://www.cnblogs.com/peasana/archive/2012/02/13/null;  
  13.   
  14.             using (MemoryStream ms= new MemoryStream())  
  15.             {  
  16.   
  17.                  using (Bitmap Bitmap = new Bitmap(Image))  
  18.                 {  
  19.   
  20.                     Bitmap.Save(ms, imageFormat);  
  21.   
  22.                     ms.Position = 0;  
  23.   
  24.                     data = http://www.cnblogs.com/peasana/archive/2012/02/13/new byte[ms.Length];  
  25.   
  26.                     ms.Read(data, 0, Convert.ToInt32(ms.Length));  
  27.   
  28.                     ms.Flush();  
  29.   
  30.                 }  
  31.   
  32.             }  
  33.   
  34.             return data;  
  35.   
  36.         }  
  37.   
  38.    
  39.   
  40.    
  41.   
  42.             /// <summary>  
  43.             /// byte[]转换成Image  
  44.             /// </summary>  
  45.             /// <param name="byteArrayIn">二进制图片流</param>  
  46.             /// <returns>Image</returns>  
  47.             public static System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)  
  48.             {  
  49.                 if (byteArrayIn == null)  
  50.                     return null;  
  51.                 using (System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArrayIn))  
  52.                 {  
  53.                     System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);  
  54.                     ms.Flush();  
  55.                     return returnImage;  
  56.                 }  
  57.             }  
  58.   
  59.    
  60.   
  61.     //Image转换Bitmap  
  62.   
  63.    1. Bitmap img = new Bitmap(imgSelect.Image);  
  64.   
  65.    2. Bitmap bmp = (Bitmap)pictureBox1.Image;  
  66.   
  67.    
  68.   
  69. //Bitmap转换成Image  
  70.   
  71. using System.IO;  
  72.   
  73. private static System.Windows.Controls.Image Bitmap2Image(System.Drawing.Bitmap Bi)  
  74.         {             
  75.             MemoryStream ms = new MemoryStream();  
  76.             Bi.Save(ms, System.Drawing.Imaging.ImageFormat.Png);  
  77.             BitmapImage bImage = new BitmapImage();  
  78.             bImage.BeginInit();  
  79.             bImage.StreamSource = new MemoryStream(ms.ToArray());  
  80.             bImage.EndInit();  
  81.             ms.Dispose();  
  82.             Bi.Dispose();  
  83.             System.Windows.Controls.Image i = new System.Windows.Controls.Image();  
  84.             i.Source = bImage;  
  85.             return i ;  
  86.         }  
  87.   
  88.    
  89.   
  90. //byte[] 转换 Bitmap  
  91.  public static Bitmap BytesToBitmap(byte[] Bytes)   
  92.         {   
  93.             MemoryStream stream = null;   
  94.             try   
  95.             {   
  96.                 stream = new MemoryStream(Bytes);   
  97.                 return new Bitmap((Image)new Bitmap(stream));   
  98.             }   
  99.             catch (ArgumentNullException ex)   
  100.             {   
  101.                 throw ex;   
  102.             }   
  103.             catch (ArgumentException ex)   
  104.             {   
  105.                 throw ex;   
  106.             }   
  107.             finally   
  108.             {   
  109.                 stream.Close();   
  110.             }   
  111.         }    
  112.    
  113. //Bitmap转byte[]    
  114.         public static byte[] BitmapToBytes(Bitmap Bitmap)   
  115.         {   
  116.             MemoryStream ms = null;   
  117.             try   
  118.             {   
  119.                 ms = new MemoryStream();   
  120.                 Bitmap.Save(ms, Bitmap.RawFormat);   
  121.                 byte[] byteImage = new Byte[ms.Length];   
  122.                 byteImage = ms.ToArray();   
  123.                 return byteImage;   
  124.             }   
  125.             catch (ArgumentNullException ex)   
  126.             {   
  127.                 throw ex;   
  128.             }   
  129.             finally   
  130.             {   
  131.                 ms.Close();   
  132.             }   
  133.         }   
  134.     }   

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值