将 Stream 转成 byte[]

/// <summary>
/// 将 Stream 转成 byte[]
/// </summary>
public byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, , bytes.Length); // 设置当前流的位置为流的开始
stream.Seek(, 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, , bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(, 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, , bytes.Length);
fileStream.Close();
// 把 byte[] 转换成 Stream
Stream stream = new MemoryStream(bytes);
return stream;
}

Stream与byte转换的更多相关文章

  1. C# 流与文件(Stream & File & byte[])

    原文:https://www.cnblogs.com/long-gengyun/archive/2010/03/28/1698681.html 文件概述  文件在操作时表现为流,即流是从一些输入中读取 ...

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

    一. 二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream( ...

  3. Stream 和 byte[] 之间的转换

    Stream 和 byte[] 之间的转换 一. 二进制转换成图片 ? 1 2 3 4 5 MemoryStream ms = new MemoryStream(bytes); ms.Position ...

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

    一. 二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream( ...

  5. C# 之 Stream 和 byte[] 的相关转换

    1.二进制转换为图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream(m ...

  6. C#实现Stream与byte[]之间的转换实例教程

    一.二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream(m ...

  7. 将String转化成Stream,将Stream转换成String, C# Stream 和 byte[] 之间的转换(文件流的应用)

    static void Main( string[] args ) { string str = "Testing 1-2-3"; //convert string 2 strea ...

  8. Stream Byte[] 转换

    public byte[] StreamToBytes(Stream stream) { byte[] bytes = new byte[stream.Length]; stream.Read(byt ...

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

    引言      文件的传输和读写通常都离不开Byte,Stream,File这个类,这里我简单封装一下,方便使用. 帮助类     public static class FileHelper { / ...

随机推荐

  1. eclipse 使用maven 创建纯spring mvc项目

    接着eclipse 使用maven 创建web3.1项目 创建完成后, 讲spring mvc加入到项目中 先修改pom.xml文件 注意红色字部分 <project xmlns="h ...

  2. java中数组与List相互转换的方法

    1.List转换成为数组.(这里的List是实体是ArrayList) 调用ArrayList的toArray方法. toArray public <T> T[] toArray(T[] ...

  3. BaseAdapter导致notifyDataSetChanged()无效的三个原因及处理方法

    原文  http://blog.csdn.net/dawanganban/article/details/21376979 前一段时间在做一个项目的时候遇到了一个关于BaseAdapter的notif ...

  4. python 学习day6(面向对象)

    博客部分内容转自:http://www.cnblogs.com/wupeiqi/p/4493506.html 面向对象编程介绍 为什么要用面向对象进行开发? 面向对象的特性:封装.继承.多态 类.方法 ...

  5. 为每个页面加上Session判断 转

    首先新建一个类,继承自System.Web.UI.Page,然后重写OnInit,如下:   using System; using System.Data; using System.Configu ...

  6. HttpContext.Current.Cache 过期时间

    原文:HttpContext.Current.Cache 过期时间 为了更快的读取数据,我们一般会把常用到的数据加载到Cache中 在.NET中,Cache的存在可以依赖多中方式,主要用到HttpCo ...

  7. C#中IList<T>与List<T>的区别感想【转】

    写代码时对: IList IList11 =new List (); List List11 =new List (); 有所疑惑,于是在网上搜索一下,很受启发,于是收藏下来,但对部分观点不敢苟同,用 ...

  8. httpUrlConnection的參数具体解释

    post方式的的请求过程: // 设置是否向httpUrlConnection输出,由于这个是post请求,參数要放在 // http正文内,因此须要设为true, 默认情况下是false; http ...

  9. [置顶] java web 动态服务器

    写了一个java web 动态服务器,主要通过内部类来实现,动态类使用了外部类,采用了 classforname 实例化,动态类的构造方法不能带参数, 效果都出来了,分享给有需要的 朋友.判断做的不够 ...

  10. CodeForces463C Gargari and Bishops(贪心)

    CodeForces463C Gargari and Bishops(贪心) CodeForces463C 题目大意:  在国际象棋的棋盘上放两个主教,这个两个主教不能攻击到同一个格子,最后的得分是这 ...