Stream 和 byte[] 之间的转换

北京网站建设-恒动时空
一. 二进制转换成图片

MemoryStream ms = new MemoryStream(bytes);
ms.Position = 0;
Image img = Image.FromStream(ms);
ms.Close();
this.pictureBox1.Image

二. C#中byte[]与string的转换代码
1.

System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
byte[] inputBytes =converter.GetBytes(inputString);
string inputString = converter.GetString(inputBytes);

2.

string inputString = System.Convert.ToBase64String(inputBytes);
byte[] inputBytes = System.Convert.FromBase64String(inputString);
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);

三. C# Stream 和 byte[] 之间的转换

/// 将 Stream 转成 byte[]

public byte[] StreamToBytes(Stream stream)
{
    byte[] bytes = new byte[stream.Length];
    stream.Read(bytes, 0, bytes.Length);
    // 设置当前流的位置为流的开始
    stream.Seek(0, SeekOrigin.Begin);
    return bytes;
}

/// 将 byte[] 转成 Stream

public Stream BytesToStream(byte[] bytes)
{
    Stream stream = new MemoryStream(bytes);
    return stream;
}

四. Stream 和 文件之间的转换

将 Stream 写入文件

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

五. 从文件读取 Stream

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;

}

//Bitmap 转化为 Byte[]
                Bitmap BitReturn = new Bitmap();
                byte[] bReturn = null;
                MemoryStream ms = new MemoryStream();
                BitReturn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                bReturn = ms.GetBuffer();
时间: 2024-08-06 18:36:07

Stream 和 byte[] 之间的转换的相关文章

C# Stream 和 byte[] 之间的转换

原文:C# Stream 和 byte[] 之间的转换 Stream 和 byte[] 之间的转换 /* - - - - - - - - - - - - - - - - - - - - - - - - * Stream 和 byte[] 之间的转换 * - - - - - - - - - - - - - - - - - - - - - - - */ /// <summary> /// 将 Stream 转成 byte[] /// </summary> public byte[] S

C# Stream 和 byte[] 之间的转换(文件流的应用)

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

关于Stream和byte之间的转换(from www.sysoft.cc)

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

Drawable、Bitmap、byte[]之间的转换

1.Drawable → Bitmap Java代码   public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap .createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARG

Image与byte[]之间的转换

//将image转化为二进制 public static byte[] GetByteImage(Image img) { byte[] bt = null; if (!img.Equals(null)) { using (MemoryStream mostream = new MemoryStream()) { Bitmap bmp = new Bitmap(img); bmp.Save(mostream, System.Drawing.Imaging.ImageFormat.Bmp);//将

C#--整型与字节数组byte[]之间的转换

转:https://www.cnblogs.com/dayang12525/p/6393941.html using System; int  i = 123;byte [] intBuff = BitConverter.GetBytes(i);     // 将 int 转换成字节数组lob.Write(intBuff, 0, 4);i = BitConverter.ToInt32(intBuff, 0);           // 从字节数组转换成 int double x = 123.45

Android Drawable、Bitmap、byte[]之间的转换

转自http://blog.csdn.net/june5253/article/details/7826597 1.Bitmap-->Drawable Bitmap drawable2Bitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof NinePatch

Image 和byte[]之间的转换

1.Image 转 byte[] public byte[] GetByteByImage(Image image) { byte[] bt = null; try { if (!image.Equals(null)) { MemoryStream ms = new MemoryStream(); Bitmap bmp = new Bitmap(image); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); bt = new byte[

编码和解码(字符串与byte[]之间的转换)

资源来自互联网http://www.cnblogs.com/dabaopku/archive/2012/02/27/2370446.html 非常蛋疼的事情, google 和 baidu 在编码是分别采用了 UTF-8 和 GB2312 基础知识 UTF-8中,一个汉字对应三个字节,GB2312中一个汉字占用两个字节. 不论何种编码,字母数字都不编码,特殊符号编码后占用一个字节. public static string MyUrlDeCode(string str, Encoding enc