[转]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;
}
/// <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[] 之间的转换相关推荐
- Stream 和Byte[] 之间的转换
代码 1 //Stream 和Byte[]之间的转换 2 byte[] arr=new byte[stream.Length];//设定arr长度 3 4 stream.Read(arr,0 ...
- C# Stream 和 byte[] 之间的转换(文件流的应用)
一. 二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = 0; Image img = Image.FromStream ...
- C# Stream 和 byte[] 之间的转换
/// <summary> /// 将 Stream 转成 byte[] /// </summary> public byte[] StreamToBytes(Stream s ...
- String类以及String与基本数据类型/char[]/byte[]之间的转换
String类: 1.String 声明为final的,不可被继承 2.String实现了Serializable接口:表示字符串是支持序列化的. 实现了Comparable接口:表示String可以 ...
- string与byte[]之间的转换
在使用.NET加密类的时候,会经常用到string与byte[]之间的转换. 首先引用System.Text命名空间,然后 UnicodeEncoding unicode=new UnicodeEnc ...
- C# string 和byte[]之间的转换
c#将string和byte数组之间互相转换 如下方法将字符串转换为byte数组,使用System.Buffer.BlockCopy方法. static byte[] GetBytes(strin ...
- 字符串与byte[]之间的转换
一. 编码 同一个字符在不同的编码下会被编成不同长度的编码,比如: ACSII,每个字符对应一个字节,实际上只使用了7位,从00h-7Fh.只能表达128个字符. GB2312,中文的一种编码,每个 ...
- Drawable、Bitmap、byte[]之间的转换
1.Drawable → Bitmap Java代码 public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bi ...
- byte[]与Image Image与 byte[] 之间的转换
/// <summary> /// 将byte[]转换为Image /// </summary> /// <param name="bytes"> ...
最新文章
- 独家 | kaggle季军新手笔记:利用fast.ai对油棕人工林图像进行快速分类(附代码)...
- 两个超长正整数的加法c语言,二个超长正整数的相加
- Last-Modified、If-Modified-Since 实现缓存和 OutputCache 的区别
- Pat乙级 1040 有几个PAT
- [C#] - 从 HTML 代码中 转换 / 提取 可读文字(PlainText)的方法
- 宏基微型计算机c650使用方法,C650卧式普通车床PLC改造
- 【鬼网络】之远程访问及控制ssh
- 查询常量、表达式、函数
- C语言小游戏 ——俄罗斯方块
- 数学_一文搞懂极大似然估计
- 用vba加载宏求地图分幅
- html如何改成花体英文字体,花体英文转换器可复制字体(附花体字发文技巧)...
- 程序猿进化 - 在拉钩子1024对APE节讲座计划
- 苹果6访问限制密码4位_破解6位密码只需4秒!3步设置密码挡住黑客
- Python安装distribute包
- 谷歌学术得到论文被引用信息
- linux系统怎么重启网卡?linux重启网卡的三种教程
- 大数据技术原理与应用之【云数据库】习题
- c语言 int 型 无符号数 %u
- 【图】深度优先遍历 广度优先遍历
热门文章
- gssh推送 Warning: Permanently added 'gitee.com’to the list of known hosts.
- Python开发技术详解PDF
- 火柴Open Day,你永远不知道你错过了什么!!
- 【温故而知新-Javascript】图片效果(图像震动效果、闪烁效果、自动切换图像)...
- LAMP配置虚拟目录
- shell---登录失败的IP添加到/etc/hosts.deny
- Linux深入篇之一:配置Nginx Web服务器及多域名主机
- RHEL5.9下cacti监控的部署详解
- 平面设计师必读的十本书
- 给特定的模拟器/设备实例发送命令