byte,bitmap,image互转

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

时间: 2024-11-07 14:08:46

byte,bitmap,image互转的相关文章

C# byte[]与char[]、string与char[]、byte[] 与 string 互转

1. byte array -> char array Byte[] b=new byte[5]{0x01,0x02,0x03,0x04,0x05}; Char[] c=Encoding.ASCII.GetChars(b); 2. char array -> byte array Char[] c=new char[5]{a,b,c,d,e}; Byte[] b=Encoding.Default.GetBytes(c); Char[] c=new char[5]{a,b,c,d,e}; Byt

C# Byte[] 和 T 互转

private T BytesToT<T>(byte[] bytes) { using (var ms = new MemoryStream()) { ms.Write(bytes, 0, bytes.Length); var bf = new BinaryFormatter(); ms.Position = 0; var x = bf.Deserialize(ms); return (T)x; } } private byte[] TToBytes<T>(T obj) { var

java中byte与int互转

package com.yl.common.utils; /** * byte转换工具 * * @author huangzp * @date 2015-6-09 */ public class ByteUtil { /** * 将iSource转为长度为iArrayLen的byte数组,字节数组的低位是整型的低字节位 * @param iSource * @param iArrayLen * @return */ public static byte[] toByteArray(int iSo

【转】Drawable /Bitmap、String/InputStream、Bitmap/byte[]

原文:http://wuxiaolong.me/2015/08/10/Drawable-to-Bitmap/ Drawable互转Bitmap Drawable转Bitmap 1234 Resources res = getResources();Drawable drawable = res.getDrawable(R.drawable.myimage);BitmapDrawable bd = (BitmapDrawable) d;Bitmap bm = bd.getBitmap(); 123

C# Bitmap类型与Byte[]类型相互转化

Bitmap   =>   byte[] Bitmap b = new Bitmap( "test.bmp "); MemoryStream ms = new MemoryStream(); b.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp); byte[] bytes= ms.GetBuffer(); //byte[] bytes= ms.ToArray(); 这两句都可以,至于区别么,下面有解释 ms.Close(); byte

Bitmap,byte[],Drawable相互转化

1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象 2.Canvas画布,绘图的目的区域,用于绘图 3.Bitmap位图,用于图的处理 4.Matrix矩阵 1.从资源中获取Bitmap 1 Resources res = getResources();2 Bitmap bmp = BitmapFactory.decode

Android Drawable、Bitmap、byte[]之间的转换

转自http://blog.csdn.net/june5253/article/details/7826597 1.Bitmap-->Drawable Bitmap drawable2Bitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof NinePatch

Drawable、Bitmap、byte[]之间的转换

1.Drawable → Bitmap Java代码   public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap .createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARG

Android中Bitmap,byte[],Drawable相互转化

一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象 2.Canvas画布,绘图的目的区域,用于绘图 3.Bitmap位图,用于图的处理 4.Matrix矩阵 二.Bitmap 1.从资源中获取Bitmap 1 Resources res = getResources();2 Bitmap bmp = Bitm