/// <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;
}
详细出处参考:http://www.jb51.net/article/13706.htm

C# Stream 和 byte[] 之间的转换相关推荐

  1. Stream 和Byte[] 之间的转换

    代码  1 //Stream 和Byte[]之间的转换  2 byte[] arr=new byte[stream.Length];//设定arr长度  3   4 stream.Read(arr,0 ...

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

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

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

    Code /* - - - - - - - - - - - - - - - - - - - - - - - -   * Stream 和 byte[] 之间的转换  * - - - - - - - - ...

  4. String类以及String与基本数据类型/char[]/byte[]之间的转换

    String类: 1.String 声明为final的,不可被继承 2.String实现了Serializable接口:表示字符串是支持序列化的. 实现了Comparable接口:表示String可以 ...

  5. string与byte[]之间的转换

    在使用.NET加密类的时候,会经常用到string与byte[]之间的转换. 首先引用System.Text命名空间,然后 UnicodeEncoding unicode=new UnicodeEnc ...

  6. C# string 和byte[]之间的转换

    c#将string和byte数组之间互相转换   如下方法将字符串转换为byte数组,使用System.Buffer.BlockCopy方法. static byte[] GetBytes(strin ...

  7. 字符串与byte[]之间的转换

    一.  编码 同一个字符在不同的编码下会被编成不同长度的编码,比如: ACSII,每个字符对应一个字节,实际上只使用了7位,从00h-7Fh.只能表达128个字符. GB2312,中文的一种编码,每个 ...

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

     1.Drawable → Bitmap Java代码   public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bi ...

  9. byte[]与Image Image与 byte[] 之间的转换

    /// <summary> /// 将byte[]转换为Image /// </summary> /// <param name="bytes"> ...

最新文章

  1. Django--models一对多实例
  2. 40款奇特的名片设计,吸引大家的眼球《上篇》
  3. 矿泉水功能突破口-丰收节交易会·李喜贵:遵义谋定水产业
  4. 89C51单片机定时器控制的流水灯
  5. [渝粤教育] 厦门大学 大数据技术原理与应用 参考 资料
  6. 设计模式--单例模式--Java实现
  7. 小屏党失望!iPhone 13四款机型,mini版没了
  8. JavaScript多线程 html5 Worker, SharedWorker
  9. flash与javacript:图片交互
  10. 实验五:编写、调试具有多个段的程序
  11. php中$t=date()函数参数意义及时间更改
  12. php下载功能的实现,laravel实现下载文件
  13. 单点登录原理以及简单实现
  14. UVa 10015 - Joseph's Cousin
  15. 计算机内存大小rom,rom容量(ram容量和rom容量谁大)
  16. Java并发HashSet报错ConcurrentModificationException解决方案
  17. ViewBinding 与 Kotlin 委托双剑合璧
  18. qq宠物html代码,QQ企鹅即将消失,那个被我们所遗忘的宠物,终于要给大家说再见了...
  19. Flutter Bloc构建轻量级MVVM
  20. ECCV2020 | 论文阅读——Arbitrary-Oriented Object Detection with Circular Smooth Label

热门文章

  1. eventbus框架
  2. 网络编程学习笔记(recvmsg和sendmsg函数)
  3. Codeforces 524F And Yet Another Bracket Sequence 哈希
  4. java设计模式-建造者模式
  5. Scala(三):类
  6. LR接口测试---基于http协议之get/post
  7. Android菜单—Menu
  8. The Internet Communications Engine (Ice) 跨平台异构通讯方案 第一弹-ICE简介
  9. 内存溢出原因及解决方案
  10. 用findstr查找特定文件里面的字符串