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 (ArgumentException ex)
            {
                throw ex;
            }
            finally
            {
                stream.Close();
            }
        }   

//图片转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();
            }
        }
    }  

=====================  

* Stream 和 byte[] 之间的转换
 * - - - - - - - - - - - - - - - - - - - - - - - */
/// <summary>
/// 将 Stream 转成 byte[]
/// </summary>
public byte[] StreamToBytes(Stream stream)
{
    byte[] bytes = new byte[stream.Length];
    stream.Read(bytes, 0, bytes.Length);  

    // 设置当前流的位置为流的开始
    stream.Seek(0, SeekOrigin.Begin);
    return bytes;
}  

/// <summary>
/// 将 byte[] 转成 Stream
/// </summary>
public Stream BytesToStream(byte[] bytes)
{
    Stream stream = new MemoryStream(bytes);
    return stream;
}  

/* - - - - - - - - - - - - - - - - - - - - - - - -
 * Stream 和 文件之间的转换
 * - - - - - - - - - - - - - - - - - - - - - - - */
/// <summary>
/// 将 Stream 写入文件
/// </summary>
public void StreamToFile(Stream stream,string fileName)
{
    // 把 Stream 转换成 byte[]
    byte[] bytes = new byte[stream.Length];
    stream.Read(bytes, 0, bytes.Length);
    // 设置当前流的位置为流的开始
    stream.Seek(0, SeekOrigin.Begin);  

    // 把 byte[] 写入文件
    FileStream fs = new FileStream(fileName, FileMode.Create);
    BinaryWriter bw = new BinaryWriter(fs);
    bw.Write(bytes);
    bw.Close();
    fs.Close();
}  

/// <summary>
/// 从文件读取 Stream
/// </summary>
public Stream FileToStream(string fileName)
{
    // 打开文件
    FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
    // 读取文件的 byte[]
    byte[] bytes = new byte[fileStream.Length];
    fileStream.Read(bytes, 0, bytes.Length);
    fileStream.Close();
    // 把 byte[] 转换成 Stream
    Stream stream = new MemoryStream(bytes);
    return stream;
}  
时间: 2024-10-12 15:04:57

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

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

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

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

/// <summary>         /// 将图片Image转换成Byte[]         /// </summary>         /// <param name="Image">image对象</param> /// <param name="imageFormat">后缀名</param> /// <returns></returns> public

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

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数组与对象之间的相互转换

在进行网络通信时可能需要传输对象,如果用NIO的话,只能用Bytebuffer和channel直接 通过ByteArray*Stream和Object*Stream可以将byte数组和对象进行相互的转换. 1.byte数组转对象: byte [] data=initData();//初始化byte数组 ByteArrayInputStream inputStream=new ByteArrayInputStream(data); ObjectInputStream oInputStream=ne

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

1.将图片Image转换成Byte[] /// <summary>        /// 将图片Image转换成Byte[]        /// </summary>        /// <param name="Image">image对象</param>        /// <param name="imageFormat">后缀名</param>        /// <ret

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

Python网络编程——主机字节序和网络字节序之间的相互转换

If you ever need to write a low-level network application, it may be necessary to handle the low-level data transmission over the wire between two machines. This operation requires some sort of conversion of data from the native host operating system

C# char[]与string之间的相互转换

string 转换成 Char[] string ss = "abcdefg"; char[] cc = ss.ToCharArray(); Char[] 转换成string string s = new string(cc); byte[] 与 string 之间的转换 byte[] bb = Encoding.UTF8.GetBytes(ss); string s = Encoding.UTF8.GetString(bb); string[] 转换成string string st