Byte[]和Stream相互转换
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[] 转成 Stream
public
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();
}
五. 从文件读取 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[] 转换成 Stream
Stream stream = new
MemoryStream(bytes);
return stream;
}
Byte[]和Stream相互转换的更多相关文章
- C#:文件、byte[]、Stream相互转换
一.byte[] 和 Stream /// <summary> /// byte[]转换成Stream /// </summary> /// <param name=&q ...
- byte[] Base64 Stream 之间相互转换
图片 base64转byte[] /// <summary> /// 保存base64图片,返回阿里云地址 /// </summary> /// <param name= ...
- golang []byte 和 string相互转换
原文链接:golang []byte和string相互转换 测试例子 package main import ( "fmt" ) func main() { str2 := &qu ...
- 16进制字符串和byte数组进行相互转换\将10进制转换为任意进制
16进制字符串和byte数组进行相互转换 简介 1个byte对应8个bit,16进制使用4个bit,所以一个byte转成16进制,占用两位. JAVA代码 private static final c ...
- InputStream只能读取一次的解决办法 C# byte[] 和Stream转换
x 情景--->>> 导入文件的时候,前台传过来一个文件, 后台接到: HttpPostedFileBase file = Request.Files[];由于对这个文件后台处理比较 ...
- C#中字节数组(byte[])和字符串相互转换
转换过程主要使用到System.Text.Encoding命名空间下的类 1. 字符串转换成字节数组byte[]: string str = "This is test string&quo ...
- C#-----字节数组(byte[])和字符串相互转换
Encoding类 表示字符编码 1.字符串转换成字节数组byte[] using System; using System.Collections.Generic; using System ...
- golang []byte和string相互转换
测试例子 package main import ( "fmt" ) func main() { str2 := "hello" ...
- 数组Byte [] 和 string 相互转换
using System; using System.Collections.Generic; using System.Text; namespace NET.MST.Fourth.StringBy ...
随机推荐
- MonkeyTalk使用方法
1.简单介绍 MonkeyTalk软件测试工具由两部分构成:MonkeyTalk IDE 和 MonkeyTalk Agents MonkeyTalk IDE是Eclipse平台的工具,工能是:对iO ...
- springmvc Cacheable
直接上代码: <cache:annotation-driven /> <bean id="cacheManager" class="org.spring ...
- BIO、NIO和AIO
BIO(Blocking I/O)同步阻塞I/O 这是最基本与简单的I/O操作方式,其根本特性是做完一件事再去做另一件事,一件事一定要等前一件事做完,这很符合程序员传统的顺序来开发思想,因此BIO模型 ...
- MyBatis中大于和小于号的转义写法
mybatis 中 SQL 写在mapper.xml文件中,而xml解析 < .>.<=.>= 时会出错,这时应该使用转义写法. 方式一 < <= > > ...
- Java——包装类(Wrapper)
2.7包装类(Wrapper) 基本数据类型由于不是类,不能够使用java类库里提供的大量的方法.所有在设计上,我们让每一个基本数据类型都对应一个类,同时数据存储的范围还不变.此时相当于基本数据类型就 ...
- thinkphp 表单合法性检测
在处理表单提交的数据的时候,建议尽量采用Think\Model类提供的create方法首先进行数据创建,然后再写入数据库. 大理石平台厂家 create方法在创建数据的同时,可以进行更为安全的处理操作 ...
- 51nod 1627 瞬间移动(组合数学)
传送门 解题思路 因为每次横纵坐标至少\(+1\),所以可以枚举走的步数,枚举走的步数\(i\)后剩下的就是把\(n-1\)与\(m-1\)划分成\(i\)个有序正整数相加,所以用隔板法,\(ans= ...
- java 直接调用micorosoft office (2003-2007和2010版本)中excel中计算函数
主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx 读取excel和MySQL相关: java的poi技 ...
- NX二次开发-UFUN点收集器UF_UI_select_point_collection
#include <uf.h> #include <uf_ui.h> UF_initialize(); //点收集器 char sMessage[] = "点收集器& ...
- spring-helloworld (1)
目录 一.eclipse安装springsource-tools插件 二.新建maven工程,引入spring配置 三.添加helloworld类 四.使用springsource-tools插件 创 ...