Byte[]、Image、Bitmap_之间的相互转换
1.将图片Image转换成Byte[]
///
<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 = http://www.cnblogs.com/peasana/archive/2012/02/13/null;
using (MemoryStream ms= new MemoryStream())
{
using (Bitmap Bitmap = new Bitmap(Image))
{
Bitmap.Save(ms, imageFormat);
ms.Position = 0;
data = http://www.cnblogs.com/peasana/archive/2012/02/13/new byte[ms.Length];
ms.Read(data, 0, Convert.ToInt32(ms.Length));
ms.Flush();
}
}
return data;
}
2. byte[]转换成Image
/// <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;
}
}
3. Image转换Bitmap
//Image转换Bitmap
1.
Bitmap img = new Bitmap(imgSelect.Image);
2.
Bitmap bmp = (Bitmap)pictureBox1.Image;
4. Bitmap转换成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 ;
}
5. byte[] 转换 Bitmap
//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();
}
}
6. Bitmap转byte[]
//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[]、Image、Bitmap_之间的相互转换的更多相关文章
- android开发之Bitmap 、byte[] 、 Drawable之间的相互转换
一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...
- Byte[]、Image、Bitmap 之间的相互转换
原文:Byte[].Image.Bitmap 之间的相互转换 /// <summary> /// 将图片Image转换成Byte[] /// </summ ...
- Python网络编程——主机字节序和网络字节序之间的相互转换
If you ever need to write a low-level network application, it may be necessary to handle the low-lev ...
- 字符编码之间的相互转换 UTF8与GBK(转载)
转载自http://www.cnblogs.com/azraelly/archive/2012/06/21/2558360.html UTF8与GBK字符编码之间的相互转换 C++ UTF8编码转换 ...
- 【miscellaneous】【C/C++语言】UTF8与GBK字符编码之间的相互转换
UTF8与GBK字符编码之间的相互转换 C++ UTF8编码转换 CChineseCode 一 预备知识 1,字符:字符是抽象的最小文本单位.它没有固定的形状(可能是一个字形),而且没有值." ...
- strconv:各种数据类型和字符串之间的相互转换
介绍 strconv包实现了基本数据类型和其对应字符串之间的相互转换.主要有一下常用函数:Atoi,Itoa,Parse系列,Formart系列,Append系列 string和int之间的转换 这一 ...
- C# Enum Name String Description之间的相互转换
最近工作中经常用到Enum中Value.String.Description之间的相互转换,特此总结一下. 1.首先定义Enum对象 public enum Weekday { [Descriptio ...
- 速战速决 (6) - PHP: 获取 http 请求数据, 获取 get 数据 和 post 数据, json 字符串与对象之间的相互转换
[源码下载] 速战速决 (6) - PHP: 获取 http 请求数据, 获取 get 数据 和 post 数据, json 字符串与对象之间的相互转换 作者:webabcd 介绍速战速决 之 PHP ...
- json和string 之间的相互转换
json和string 之间的相互转换 <script type="text/javascript"> //先认识一下js中json function showInfo ...
随机推荐
- FileWriter类的flush方法的作用
FileWriter类的flush方法的作用 每次io都会影响性能,将需要写入的内容,放入缓冲区中,然后调用flush方法,将缓冲区内容写入文件中.
- 遍历aspx页面中所有的指定控件
//1.遍历页面中所有的TextBox,并将值设置成String.Empty for (int j = 0; j < this.Controls.Count; j++){ foreac ...
- xp snapshot.
snap current active window(alt + Print Screen SysRq). snap the whole window (Print Screen SysRq).
- 抓取锁的sql语句-第六次修改
增加异常处理 CREATE OR REPLACE PROCEDURE SOLVE_LOCK AS V_SQL VARCHAR2(3000); --定义 v_sql 接受抓取锁的sql语句V_SQL02 ...
- AlertView + Block 的使用
AlertView的使用还是很频繁的,扁平化以后似乎也不是那么丑了,但是到现在为止官方还是在用Delegate来处理点击事件,很影响程序的可读性. 如果用Block那么就会一目了然.自己本来想写一个, ...
- C#字符串string的常用使用方法
1--->字符串的声明: 1.string s=new string(char[] arr) //根据一个字符数组声明字符串,即将字符字组转化为字符串. 2.string s=new s ...
- sql -实验二
8. 统计各部门下工资大于2000的雇员的平均工资. select avg(sal)from empwhere sal>2000;
- android布局2
Android功能强大,界面华丽,但是众多的布局属性就害苦了开发者,下面这篇文章结合了网上不少资料. 第一类:属性值为true或false android:layout_centerHrizontal ...
- Jquery的外部链接和编写样式
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></ ...
- winform批量查询单号剔除重复
//查询分单函数 private string GetQueryInSubbillNo() { string strSubbillNO = " ...