C# 二进制与Stream转换
来源:互联网 发布:淘宝首页海报图片尺寸 编辑:程序博客网 时间:2024/06/07 13:12
C# Stream 和 byte[] 之间的转换 一. 二进制转换成图片MemoryStream ms = new MemoryStream(bytes);ms.Position = 0;Image img = Image.FromStream(ms);ms.Close();this.pictureBox1.Image二. C#中byte[]与string的转换代码1、System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding(); byte[] inputBytes =converter.GetBytes(inputString); string inputString = converter.GetString(inputBytes);2、string inputString = System.Convert.ToBase64String(inputBytes); byte[] inputBytes = System.Convert.FromBase64String(inputString);FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);三. C# Stream 和 byte[] 之间的转换/// 将 Stream 转成 byte[]public byte[] StreamToBytes(Stream stream) { byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); // 设置当前流的位置为流的开始 stream.Seek(0, SeekOrigin.Begin); return bytes; }/// 将 byte[] 转成 Streampublic Stream BytesToStream(byte[] bytes) { Stream stream = new MemoryStream(bytes); return stream; }四. Stream 和 文件之间的转换将 Stream 写入文件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(); }五. 从文件读取 Streampublic 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; }
0 0
- C# 二进制与Stream转换
- C#二进制格式与文件相互转换
- C#字符串与二进制相互转换
- c#图片与二进制流相互转换
- C#实现Stream与byte[]之间的转换实例教程
- stream 与 byte[] 转换
- Stream与String转换
- 字符串与Stream转换
- c#中文件与二进制流文件的转换
- 用C#代码实现二进制与十进制的互相转换
- c#中文件与二进制流文件的转换
- c# word文档与二进制数据的相互转换
- c#中文件与二进制流文件的转换
- c# word文档与二进制数据的相互转换
- C# 文件与二进制流间的转换
- 用C#代码实现二进制与十进制的互相转换
- C# 文件与二进制流间的转换
- C# 文件与二进制流间的转换
- shell十三问之1: 何为shell?
- (hdu step 5.1.4)Farm Irrigation(在两个节点合并有限制条件的情况下,求集合的个数)
- Codeforces Round #295 (Div. 1) (A B)
- 1087. All Roads Lead to Rome (30)
- Windows下CMD目录切换问题
- C# 二进制与Stream转换
- Python全局变量的隐藏“窍门”
- 1088. Rational Arithmetic (20)
- TCP注册端口号大全7
- 风火轮
- HDU 1011 Starship Troopers
- V8引擎之从webcore到V8
- 对象的初始化过程
- 冈于惨壕霞谫杀巫蕉然抗琅虑瘴实