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

  }

  }

}

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

  1. Bitmap byte[] InputStream Drawable 互转

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStrea ...

  2. java中byte和blob互转

    1. btye[]转blob byte[] bs = ... Blob blob = conn.createBlob(); blob.setBytes(1, bs); ps.setBlob(2, bl ...

  3. 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.AS ...

  4. Bitmap Byte[] 互转

    严正声明:作者:psklf出处: http://www.cnblogs.com/psklf/p/5889978.html欢迎转载,但未经作者同意,必须保留此段声明:必须在文章中给出原文连接:否则必究法 ...

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

    原文:http://wuxiaolong.me/2015/08/10/Drawable-to-Bitmap/ Drawable互转Bitmap Drawable转Bitmap 1234 Resourc ...

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

    转自http://blog.csdn.net/june5253/article/details/7826597 1.Bitmap-->Drawable Bitmap drawable2Bitma ...

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

    一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...

  8. 【Android】[转] Android中Bitmap,byte[],Drawable相互转化

    一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...

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

    Bitmap   =>   byte[]  Bitmap b = new Bitmap( "test.bmp "); MemoryStream ms = new Memory ...

随机推荐

  1. 弦图的判定MCS算法(zoj1015)

    题意:裸的弦图的判定: 弦图定义:给出一个无向连通图,如果每个环中都存在至少一条弦(环中存在不相邻的两点直接相连)这样的图叫做弦图: 转载:http://blog.csdn.net/crux_d/ar ...

  2. POJ 2947 Widget Factory(高斯消元)

    Description The widget factory produces several different kinds of widgets. Each widget is carefully ...

  3. [ubuntu] ubuntu13.04安装rabbitcvs管理svn

    加入源 sudo add-apt-repository ppa:rabbitvcs/ppa 更新 sudo apt-get update 安装软件 sudo apt-get install rabbi ...

  4. 【rails3教材】博客构建过程2

    2. 使用脚手架快速搭建网页 rails的脚手架可以快速生成应用程序的一些片段,如果你需要为一个资源创建一系列的控制器视图模型,那么脚手架就是你需要的工具 3. 创建资源 对于一个博客程序,你可以以生 ...

  5. 最懂中文的H5前端框架amazeUI

    Amaze UI 是一个轻量级(所有 CSS 和 JS gzip 后 100 kB 左右)的前端框架, 基于开源社区流行前端框架编写 amazeUI的网址:http://amazeui.org/get ...

  6. 使用Json.Net处理json序列化和反序列化接口或继承类

    以前一直没有怎么关注过Newtonsoft的Json.Net这个第三方的.NET Json框架,主要是我以前在开发项目的时候大多数使用的都是.NET自带的Json序列化类JavaScriptSeria ...

  7. Spring之AOP面向切片

       一.理论基础: AOP(Aspectoriented programming)面向切片/服务的编程,在Spring中使用最多的是对事物的处理.而AOP这种思想在程序中很多地方可以使用的,比如说, ...

  8. RAII惯用法详解

    [1]什么是RAII惯用法? RAII是Resource Acquisition Is Initialization的缩写,意为“资源获取即初始化”. 它是C++之父Bjarne Stroustrup ...

  9. msyql数据库位置

    MySQL默认的数据文件存储目录为/var/lib/mysql.假如要把目录移到/home/data下需要进行下面几步: 1.home目录下建立data目录 cd /home mkdir data 2 ...

  10. eclipse字体颜色设置

    修改编码:window-->perference--->General--> Configure.--> Configure.-->workspace修改编辑背景色:wi ...