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 ...
随机推荐
- jq容易混淆点
jQuery链式编程: 原:把当前元素颜色修改为红色,其余兄弟颜色不变 $(this).css("color",'red') ; $(this).siblings().css(&q ...
- 2019_7_31python
练习 输入三条边长如果能构成三角形就计算周长和面积 import math a,b,c = input().split(',') a = float(a) b = float(b) c = floa ...
- Java——package和import关键字
1.8 package和import关键字 1.8.1 package 包其实就是目录,特别是项目比较大,java 文件特别多的情况下,我们应该分目录管理,在java 中称为分包管理,包名称通常采用小 ...
- NX二次开发-C++的vector排序去重用法
#include <algorithm> //vector排序去重 sort( BoxNum.begin(), BoxNum.end()); BoxNum.erase(unique(Box ...
- Gym101981D - 2018ACM-ICPC南京现场赛D题 Country Meow
2018ACM-ICPC南京现场赛D题-Country Meow Problem D. Country Meow Input file: standard input Output file: sta ...
- Python 爬虫-爬取京东手机页面的图片
具体代码如下: __author__ = 'Fred Zhao' import requests from bs4 import BeautifulSoup import os from urllib ...
- 隐藏tomcat页面异常显示的版本信息
1.正常情况下,tomcat遇到404或500会返回版本信息: 2.有时我们不需要暴露版本信息,像这样: 3.只需要修改apache-tomcat-7.0.59的lib目录下的catalina.jar ...
- ubantu 编译mysql++
sudo apt-get install mysql-server mysql-client 指定mysql-lib位置: ./configure --with-mysql-lib=/usr/lib/ ...
- spark入门到精通(后续开始学习)
早几年国内外研究者和业界比较关注的是在 Hadoop 平台上的并行化算法设计.然而, HadoopMapReduce 平台由于网络和磁盘读写开销大,难以高效地实现需要大量迭代计算的机器学习并行化算法. ...
- 3.2_springBoot2.1.x检索之JestClient操作ElasticSearch
这里介绍Jest方式交互, 导入jest版本 <!--导入jest--> <dependency> <groupId>io.searchbox</groupI ...