C#中 byte[] Image Bitmap之间的相互转化

/// <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;

}

///
<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();

}

}

}

C#中 byte[] Image Bitmap之间的相互转化

时间: 2024-10-29 02:00:36

C#中 byte[] Image Bitmap之间的相互转化的相关文章

Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】

package com.soai.imdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.grap

Byte[]、Image、Bitmap 之间的相互转换

原文:Byte[].Image.Bitmap 之间的相互转换 /// <summary>        /// 将图片Image转换成Byte[]        /// </summary>        /// <param name="Image">image对象</param>        /// <param name="imageFormat">后缀名</param>        

C# Byte[]、Image、Bitmap 之间的相互转换

//byte[] 转图片 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 (ArgumentExceptio

Python中的列表,元组,字符串之间的相互转化

Python中的列表元组和字符串之间的相互转化需要利用,tuple(),list(),str(). 示例如下: >>> the_string = "hello I'am xiaoli!" >>> #字符串转化为元组 >>> the_tuple = tuple(the_string) >>> the_tuple ('h', 'e', 'l', 'l', 'o', ' ', 'I', "'", 'a

android那些事之Bitmap、InputStream、Drawable、byte[]、Base64之间的转换关系

1 // 将Bitmap转换成InputStream(压缩率quality.100表示不压缩.10表示压缩90%) 2 public InputStream Bitmap2InputStream(Bitmap bm, int quality) { 3 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 4 bm.compress(Bitmap.CompressFormat.PNG, quality, baos); 5 InputSt

byte[],bitmap,drawable之间的相互转换

Byte[]转Bitmap BitmapFactory.decodeByteArray(data, 0, data.length); Bitmap转Byte[] ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); data2 = baos.toByteArray(); 可以选择图片格式,这里是JPEG,可以用PNG Bit

C#中二进制和流之间的各种相互转换

一. 二进制转换成图片间的转换 1 2 3 4 5 MemoryStream ms = new MemoryStream(bytes); ms.Position = 0; Image img = Image.FromStream(ms); ms.Close(); this.pictureBox1.Image 二. C#中byte[]与string的转换代码 1. 1 2 3 System.Text.UnicodeEncoding converter = new System.Text.Unico

Android开发实用技巧:Drawable和Bitmap之间不得不说的秘密

Bitmap - 称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565.RGB888.作为一种逐像素的显示对象执行效率高,但是缺点也很明显存储效率低.我们理解为一种存储对象比较好. Drawable - 作为Android平下通用的图形对象,它可以装载常用格式的图像,比如GIF.PNG.JPG,当然也支持BMP,当然还提供一些高级的可视化对象,比如渐变.图形等. 一. Bitmap转Drawable Bitmap bm = xxx; //xxx根据你的情况获取 Bitmap

【转】java中byte数组与int类型的转换(两种方式)----不错

原文网址:http://blog.csdn.net/piaojun_pj/article/details/5903009 java中byte数组与int类型的转换,在网络编程中这个算法是最基本的算法,我们都知道,在socket传输中,发送.者接收的数据都是 byte数组,但是int类型是4个byte组成的,如何把一个整形int转换成byte数组,同时如何把一个长度为4的byte数组转换为int类型.下面有两种方式. 第一种方法: public static byte[] int2byte(int