Code
/* - - - - - - - - - - - - - - - - - - - - - - - - 
 * 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;
}

转载于:https://www.cnblogs.com/treeyh/archive/2008/11/26/1341234.html

[转]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. C# Stream 和 byte[] 之间的转换

    /// <summary> /// 将 Stream 转成 byte[] /// </summary> public byte[] StreamToBytes(Stream s ...

  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. 独家 | kaggle季军新手笔记:利用fast.ai对油棕人工林图像进行快速分类(附代码)...
  2. 两个超长正整数的加法c语言,二个超长正整数的相加
  3. Last-Modified、If-Modified-Since 实现缓存和 OutputCache 的区别
  4. Pat乙级 1040 有几个PAT
  5. [C#] - 从 HTML 代码中 转换 / 提取 可读文字(PlainText)的方法
  6. 宏基微型计算机c650使用方法,C650卧式普通车床PLC改造
  7. 【鬼网络】之远程访问及控制ssh
  8. 查询常量、表达式、函数
  9. C语言小游戏 ——俄罗斯方块
  10. 数学_一文搞懂极大似然估计
  11. 用vba加载宏求地图分幅
  12. html如何改成花体英文字体,花体英文转换器可复制字体(附花体字发文技巧)...
  13. 程序猿进化 - 在拉钩子1024对APE节讲座计划
  14. 苹果6访问限制密码4位_破解6位密码只需4秒!3步设置密码挡住黑客
  15. Python安装distribute包
  16. 谷歌学术得到论文被引用信息
  17. linux系统怎么重启网卡?linux重启网卡的三种教程
  18. 大数据技术原理与应用之【云数据库】习题
  19. c语言 int 型 无符号数 %u
  20. 【图】深度优先遍历 广度优先遍历

热门文章

  1. gssh推送 Warning: Permanently added 'gitee.com’to the list of known hosts.
  2. Python开发技术详解PDF
  3. 火柴Open Day,你永远不知道你错过了什么!!
  4. 【温故而知新-Javascript】图片效果(图像震动效果、闪烁效果、自动切换图像)...
  5. LAMP配置虚拟目录
  6. shell---登录失败的IP添加到/etc/hosts.deny
  7. Linux深入篇之一:配置Nginx Web服务器及多域名主机
  8. RHEL5.9下cacti监控的部署详解
  9. 平面设计师必读的十本书
  10. 给特定的模拟器/设备实例发送命令