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

1.将图片Image转换成Byte[]

///
<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;

}

2. byte[]转换成Image

/// <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;
              
}
           }

3.  Image转换Bitmap

//Image转换Bitmap

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

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

4. Bitmap转换成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 ;
       }

5. byte[] 转换 Bitmap

//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();
           }
       } 
6. Bitmapbyte[] 
    //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();
           }
       }
   }

时间: 2024-10-29 21:09:38

Byte[]、Image、Bitmap_之间的相互转换的相关文章

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

原文:Byte[].Image.Bitmap 之间的相互转换 /// <summary>        /// 将图片Image转换成Byte[]        /// </summary>        /// <param name="Image">image对象</param>        /// <param name="imageFormat">后缀名</param>        

byte数组与对象之间的相互转换

在进行网络通信时可能需要传输对象,如果用NIO的话,只能用Bytebuffer和channel直接 通过ByteArray*Stream和Object*Stream可以将byte数组和对象进行相互的转换. 1.byte数组转对象: byte [] data=initData();//初始化byte数组 ByteArrayInputStream inputStream=new ByteArrayInputStream(data); ObjectInputStream oInputStream=ne

Python网络编程——主机字节序和网络字节序之间的相互转换

If you ever need to write a low-level network application, it may be necessary to handle the low-level data transmission over the wire between two machines. This operation requires some sort of conversion of data from the native host operating system

C# char[]与string之间的相互转换

string 转换成 Char[] string ss = "abcdefg"; char[] cc = ss.ToCharArray(); Char[] 转换成string string s = new string(cc); byte[] 与 string 之间的转换 byte[] bb = Encoding.UTF8.GetBytes(ss); string s = Encoding.UTF8.GetString(bb); string[] 转换成string string st

3、关于String,File,InputStream之间的相互转换

1.介绍了关于String,File,InputStream之间的相互转换 1.1  String2InputStream /** * String2InputStream(String str)的工具方法 * * @param str * 需要转换的字符串str * @return 返回的是字符串str转换为inputstream的结果 */ public static InputStream String2InputStream(String str) { ByteArrayInputStr

C#List&lt;string&gt;和string[]之间的相互转换

 一.LIST概述 所属命名空间:System.Collections.Generic      public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable List<T>类是 ArrayList 类的泛型等效类.该类使用大小可按需动态增加的数组实现 IList<T> 泛型接口.  泛型的好处: 它为使

Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】

package com.soai.imdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.grap

XML编程总结(六)——使用JAXB进行java对象和xml格式之间的相互转换

(六)使用JAXB进行java对象和xml格式之间的相互转换 JAXB能够使用Jackson对JAXB注解的支持实现(jackson-module-jaxb-annotations),既方便生成XML,也方便生成JSON,这样一来可以更好的标志可以转换为JSON对象的JAVA类. JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实例文档反向生成Java对象树

java Data、String、Long三种日期类型之间的相互转换

java Data.String.Long三种日期类型之间的相互转换 // date类型转换为String类型 // formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒 // data Date类型的时间 public static String dateToString(Date data, String formatType) { return new SimpleDateFormat(formatType).format(data