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 ...
随机推荐
- Java中的浅复制和深复制 Cloneable clone
先看一个简单案例 public class Test { public static void main(String args[]) { Student stu1 = new ...
- oracle 添加自增索引
1.添加一个Sequence,此处为ID_SEQUENCE. 2.添加对应表,并设置主键 3.设置触发器 create or replace trigger sys.id_add before ins ...
- js正则表达式验证大全
/判断输入内容是否为空 function IsNull(){ var str = document.getElementById('str').value.trim(); ...
- fastjson反序列化
package cn.jsonlu.passguard.utils; import com.alibaba.fastjson.JSON; import java.lang.reflect.Type; ...
- Java枚举类型理解
Enum格式理解 Enum的格式可以看做跟class关键字一样 class的定义格式如下: public class abc{ } enum的定义格式如下: Public enum abc { } 引 ...
- IIS 配置好了,为什么网站打开一片空白?
方法如下: 进入:控制面板 - 卸载程序 - 打开或关闭Windows功能 如果访问任何不存在页面或页面出错时空白: Internet 信息服务 - 万维网服务 - 常见 HTTP 功能 - HTTP ...
- Ext4.1 tree grid的右键菜单
Ext4.1 tree grid的右键菜单功能其实挺简单的 只要添加一个itemcontextmenu事件,并在事件中显示出Menu就OK了. 代码: this.tree.on('itemcontex ...
- Oracle 创建分页存储过程(转帖)
原贴地址:http://19880614.blog.51cto.com/4202939/1316560 ps:源代码还有很多错误,我修改了 ------------------------------ ...
- Extjs中Chart利用series的tips属性设置鼠标划过时显示数据
效果如下: 从官网找到的例子,大家参考下吧.源码: Ext.require('Ext.chart.*'); Ext.require('Ext.layout.container.Fit'); Ext.o ...
- 微信公众平台Js API(WeixinApi)
微信公众平台Js API(WeixinApi): https://github.com/zxlie/WeixinApi#user-content-3%E9%9A%90%E8%97%8F%E5%BA%9 ...