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