Stream与byte转换

将 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;
} 

将 byte[] 转成 Stream

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

从文件读取 Stream

/// <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-15 00:34:19

Stream与byte转换的相关文章

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

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

C# Stream 与 byte[]、文件的转换

/* - - - - - - - - - - - - - - - - - - - - - - - - * Stream 和 byte[] 之间的转换 * - - - - - - - - - - - - - - - - - - - - - - - */ /// <summary> /// 将 Stream 转成 byte[] /// </summary> public byte[] StreamToBytes(Stream stream) {     byte[] bytes = n

【.Net】Byte,Stream,File的转换

引言      文件的传输和读写通常都离不开Byte,Stream,File这个类,这里我简单封装一下,方便使用. 帮助类     public static class FileHelper { /// <summary> ///Stream转化成byte数组 /// </summary> /// <param name="stream"></param> /// <returns></returns> publ

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; } public Stream BytesToStream(byte[] bytes) { Stream stream = new MemoryStream(b

c# int与byte[]转换

网络上总结的方法汇总: 1: public byte[] intToByte(int i) { byte[] abyte0 = new byte[4]; abyte0[0] = (byte) (0xff & i); abyte0[1] = (byte) ((0xff00 & i) >> 8); abyte0[2] = (byte) ((0xff0000 & i) >> 16); abyte0[3] = (byte) ((0xff000000 & i)

byte数组转float实现与byte转换其它类型时进行&amp;运算原理

下面是将byte数组转换为float的实现 public static float getFloat(byte[] b) { int accum = 0; accum = accum|(b[0] & 0xff) << 0; accum = accum|(b[1] & 0xff) << 8; accum = accum|(b[2] & 0xff) << 16; accum = accum|(b[3] & 0xff) << 24;