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 ...
随机推荐
- html02表格的使用
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Vijos P1325桐桐的糖果计划(有向图双连通分量)
/*重边不能删 不能删 不能删...*/ #include<iostream> #include<cstdio> #include<cstring> #define ...
- AndroidStudio SDK版本下载
错误描述: pkg: /data/local/tmp/com.example.myapplication Failure [INSTALL_FAILED_OLDER_SDK] 出现这个错误,研究了半天 ...
- 判断在Android手机内, 页面重新刷新一次
<script type="text/javascript"> var UA = navigator.userAgent.toLowerCase(); //androi ...
- Linq编程101例
原文地址:101 LINQ Samples in C# Part1 - Restriction Operators Part2 - Projection Operators Part3 - Parti ...
- Oracle11G 数据库 expdp、impdp使用示例
expdp 备份数据库.expdp/impdp简单测试 操作系统层面创建目录 [root@Oracle11g ~]# mkdir -p /home/oracle/db_back/ 修改目录的所属用户. ...
- 关于模板中的动态取值 ---反射与javascript脚本编译
在项目中经常遇到一个问题,打印word或者打印excel的时候,我们经常使用一对一的赋值或者批量替换的方式来对模板进行修改. 但是现在遇到两种场景: 1.取值是通过自定以方法进行取值的. 如:一个销售 ...
- 字符串转换成JSON(js)
JSON.parse('{"site":"zlog"}'); 使用JSON.parse需严格遵守JSON规范, 属性都需用双引号引起来, 一定是双引号!! 相反 ...
- canvas 渐变
那么第一种渐变方式就是LinearGradient,具体实施就是以下代码: var colorStyle=context.createLinearGradient(0,0,0,HEIGHT); col ...
- jquery 事件绑定(1)
$(function(){ $("#panel h5.head").bind("click",function(){ $(this).next().show() ...