C# 之 Stream 和 byte[] 的相关转换
1、二进制转换为图片
MemoryStream ms = new MemoryStream(bytes); ms.Position = 0; Image img = Image.FromStream(ms); ms.Close();
2、二进制与字符串的相互转换
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding(); byte[] inputBytes =converter.GetBytes(inputString); string inputString = converter.GetString(inputBytes);string inputString = System.Convert.ToBase64String(inputBytes); byte[] inputBytes = System.Convert.FromBase64String(inputString); FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
3、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[] 转成 Stream public Stream BytesToStream(byte[] bytes) {Stream stream = new MemoryStream(bytes);return stream; }
4、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(); }// 从文件读取Stream 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[] 转换成 StreamStream stream = new MemoryStream(bytes);return stream; }
5、Bitmap 转化为 Byte[]
//Bitmap 转化为 Byte[] Bitmap BitReturn = new Bitmap(); byte[] bReturn = null; MemoryStream ms = new MemoryStream(); BitReturn.Save(ms, System.Drawing.Imaging.ImageFormat.Png); bReturn = ms.GetBuffer();
转载于:https://www.cnblogs.com/xinaixia/p/4788077.html
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[] 之间的转换 * - - - - - - - - ...
- C# Stream 和 byte[] 之间的转换
/// <summary> /// 将 Stream 转成 byte[] /// </summary> public byte[] StreamToBytes(Stream s ...
- c# byte char string转换
c# byte char string转换 在在做一些互操作的时候往往需要一些类型的相互转换,比如用c#访问 win32api的时候往往需要向api中传入DWORD参数 即:uint参数这些数值所表示 ...
- Stream 与 byte[] 互转
public byte[] StreamToBytes(Stream stream){byte[] bytes = new byte[stream.Length];stream.Read(bytes, ...
- java byte 转 c_C 和 Java 之间的byte数据的转换问题
C语言里通常可能开发人员直接定义struct 作为数据包, 因此在java客户端接收struct 中的数据时候,受整数等类型的高低位存放的影响, 需要进行相应的转换, 参考: 转换代码如下: pack ...
- String类以及String与基本数据类型/char[]/byte[]之间的转换
String类: 1.String 声明为final的,不可被继承 2.String实现了Serializable接口:表示字符串是支持序列化的. 实现了Comparable接口:表示String可以 ...
- string与byte[]之间的转换
在使用.NET加密类的时候,会经常用到string与byte[]之间的转换. 首先引用System.Text命名空间,然后 UnicodeEncoding unicode=new UnicodeEnc ...
- java stream转byte[ ]
stream转byte[]记录,下次直接用private static byte[] steamToByte(InputStream input) throws IOException {ByteAr ...
最新文章
- /dev/rdsk 与 /dev/dsk区别
- android:使用Messenger进行进程间通信(一)
- js/jquery学习笔记
- 《大型网站技术架构》读书笔记之六:永无止境之网站的伸缩性架构
- python(15)-window7配置iPython
- 性能比拼!超详细的Tengine GEMM矩阵乘法汇编教程
- linux调度器(三)——周期性调度器与CFS
- Java和Python哪个更适合初学者的问题
- Redis--huan
- sl4a+android截屏,使用SL4A Python在android桌面上显示数据(示例)
- Google DFP广告管理系统简介:开始与您的网站进行广告集成
- windows快捷方式
- 打印一本400页的书多少钱?哪里打印书本比较便宜
- 全屏播放PPT时,播放音乐
- 量化投资学习——orderbook数据分析
- HttpClient 连接超时重试处理
- Socket心跳包机制总结
- 天宇,snapseed,第六课,静物人像
- 脏话越多,代码越好!
- 网易严选——迅速崛起的新消费品牌
热门文章
- SpriteBuilder改变布局后App运行出错代码排查
- oracle 比较日期相等
- SQL DATEADD函数
- 物化视图(materialized view) 实现数据迁移、数据定时同步
- 安装Python模块:pygame
- 又联考了一场,感觉自己好菜啊,T1没写出来,后来花了一个早上调试。QAQ。最后发现是个-1还有取模没打。。。TAT。。。难受极了!!!...
- ViewPager异常,对ViewPager源码分析
- 【leetcode】557. 反转字符串中的单词 III-原地算法
- while循环 字符串格式化 运算符 编码初识
- leaflet 的 marker 弹框 iframe 嵌套代码