/// <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. uva 10755 - Garbage Heap

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  2. linux第5天 socket api

    IPv4套接口地址结构通常也称为“网际套接字地址结构”,它以“sockaddr_in”命名,定义在头文件<netinet/in.h>中 通用地址结构用来指定与套接字关联的地址.以socka ...

  3. Hibernate的关系配置

    一对一: <hibernate-mapping> <class name="cn.jnit.bean.User" table="T_user" ...

  4. 3D语音天气球(源码分享)——创建可旋转的3D球

    开篇废话: 在9月份时参加了一个网站的比赛,比赛的题目是需要使用第三方平台提供的服务做出创意的作品. 于是我选择使用语音服务,天气服务,Unity3D,Android来制作一个3D语音天气预报,我给它 ...

  5. 夺命雷公狗---node.js---4net模块(上)

    node.js为我们提供了一个net模块,主要是为了提供了一些底层通信的小工具,包含了创建服务器/客户端方法,引入方式也很简单: var net = require('net'); net模块也为我们 ...

  6. c语言 typedef

      在C和C++编程语言中,typedef是一个关键字.它用来对一个资料类型取一个新名字.目的是为了使源代码更易于阅读和理解. 来看以下程式码: int coxes; int jaffa; ... c ...

  7. inline-block去掉空白距离的方法

    一.现象描述:inline-block形式水平呈现的元素,换行显示或空格分割的情况下,元素之间会有间距,实例如下: 使用CSS将行内元素的display设置为inline-block时,也会出现间隔: ...

  8. VS2010 断点无效肿么办?

    [1]分析原理: 1. 断点无效是因为目标文件和源文件的更新时间不一致, 编译器不认为当前的源文件是目标文件的当前版本. 2. IDE不会关心源文件的编码格式,能读入即可, 另存为Unicode或AS ...

  9. 设置Tab键为四个空格

    https://my.oschina.net/xunxun10/blog/110074

  10. JS和CSS的多浏览器兼容(2)

    2.Css的浏览器兼容性 方法一,根据不同的浏览器加载不同的css file <!DOCTYPE html>  <html> <head> <title> ...