/// <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. UML: 状态机图

    摘自http://www.umlonline.org/school/viewthread.php?tid=39 活动图将流程分解为一个一个的活动,通过活动的先后顺序来展示流程:而状态机图从某个物品的状 ...

  2. C++之路进阶——bzoj1468(tree)

    F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser  gryz2016 Logout 捐赠本站 Notice:由于本OJ ...

  3. CCF真题之图像旋转

    201503-1 问题描述 旋转是图像处理的基本操作,在这个问题中,你需要将一个图像逆时针旋转90度. 计算机中的图像表示可以用一个矩阵来表示,为了旋转一个图像,只需要将对应的矩阵旋转即可. 输入格式 ...

  4. ajax基本用法

    ajax能做到无刷新数据交互,给用户体验带来好处的同时也减小了服务器的压力,所以运用ajax能使网站性能更强劲.更吸引用户. 大型网站少不了注册页面,而大多数情况下我们不想让用户有相同的注册ID,所以 ...

  5. paper 43 :ENDNOTE下载及使用方法简介

    转载来源:http://blog.sciencenet.cn/blog-484734-367968.html 软件下载来源: EndNote v9.0 Final 正式版:http://www.ttd ...

  6. 夺命雷公狗---DEDECMS----3快速入门之隐藏

    如果我们在工作的时候遇到上操蛋的老板,本来公司是做医疗器械的,但是老板突然老了句我们不做医疗了,我们该做电影网,那么我们可以先将原本的栏目进行修改成隐藏栏目, 主要是预防变态老板突然来句“电影网更不好 ...

  7. 【crunch bang】 增加“菜单项”

    [右键菜单]->[Settings]->[Openbox]->[GUI Menu Editor] 挑选合适的位置,增加[菜单项],编辑内容.

  8. Imageloarder

    首先需要加载jar包,和在配置文件中配置Mainactivity.javapackage com.baidu.day13_2imageloader; import com.nostra13.unive ...

  9. struts2 笔记04 杂记

    流程: 1. StrutsPrepareAndExcuteFilter:StrutsPrepareAndExcuteFilter作为前端控制器,是整个Struts2的调度中心. 2. ActionMa ...

  10. Swoole 遇上 PHP会是怎样的结果呢

    一直想写点Swoole的东西,毕竟它重新定义了php,却一直不知道怎么下手写 Swoole涉及的知识点非常多,互为表里,每次想写都发现根本理不出一个头绪 Swoole是一个php的扩展,它的核心目的就 ...