/// <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. Spring中Bean的配置:基于XML文件的方式

    Bean的配置一共有两种方式:一种是基于XML文件的方式,另一种是基于注解的方式.本文主要介绍基于XML文件的方式 <bean id="helloWorld" class=& ...

  2. hdu5322 Hope

    设dp[n]为n个数字排列时候的答案,那么可以得到dp方程 dp[n]=Σdp[n-i]*c(n-1,i-1)*(i-1)!*i^2(1<=i<=n) 然后上式可以化成卷积形式,分治FFT ...

  3. centOS6.6升级gcc4.8

    最近想升级mesos0.23.0,结果编译mesos0.23.0需要gcc4.8+,可是centOS6.6最高版本的gcc也只到4.4.7版本,只好手动升级一下了. 下载4.8.2源码 wget ft ...

  4. Android activity的回传数据

    package com.example.myact3; import android.content.Intent; import android.os.Bundle; import android. ...

  5. Java内部类小程序(成员内部类,静态内部类,匿名内部类)

    /** * 测试java内部类(成员内部类,静态内部类,匿名内部类) * 局部内部类不常用,就不写了. * @package :java05 * @author shaobn * @Describe ...

  6. CSS3 Transform Matrix

    css3中的transform让我们操作变形变得很简单,诸如,translate–移动,scale–缩放,rotate–旋转,skew–斜切.这几个属性很方便,也很简单,但是其中matrix我们就不常 ...

  7. 转:Order&Shipping Transactions Status Summary

    详细内容: http://blog.csdn.net/pan_tian/article/details/7696528 WSH_DELIVERY_DETAILS.Release_Status can ...

  8. 如何在真机上调试Android应用程序(图文详解)(zz)

    http://www.cnblogs.com/lanxuezaipiao/archive/2013/03/11/2953564.html   1.首先将手机设置为调试模式 方法:设置——应用程序——开 ...

  9. PAT乙级 1005. 继续(3n+1)猜想 (25)

    1005. 继续(3n+1)猜想 (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 卡拉兹(Callatz ...

  10. 使用php模拟post的几种方法

    <?phpfunction file_get_contents_post($url,$post){$options=array('http'=>array('method'=>'PO ...