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;

}

C#图像处理:Stream 与 byte[] 相互转换,byte[]与string,Stream 与 File 相互转换等的更多相关文章

  1. Java之byte、char和String类型相互转换

    package basictype; /** * byte.char和String类型相互转换 */ public class CHJavaType { public static void main ...

  2. [.Net,C#]三类资源:流对象Stream,字节数组byte[],图片Image

    三类资源:流对象Stream,字节数组byte[],图片Image 关系:Stream<=>byte[],byte[]<=>Image Stream 与Image相互转化的媒介 ...

  3. 【C#基础】byte二进制数组转string

    //解析post请求数组返回的数组 //解码返回的二进制数组 public string DecodeBytes(byte[] c) { string html = string.Empty; try ...

  4. short a = 128, byte b = (byte)a 强制类型转换

    package 笔试; public class ShortToByte { /** * @param args */ public static void main(String[] args) { ...

  5. Byte Array to Hexadecimal String

    Lookup Text: 23,879.41 (20.8X faster) Sentence: 1.15 (23.9X faster) /// <summary> /// Hex stri ...

  6. Convert a byte[] array to readable string format. This makes the "hex" readable!

    /* * Java Bittorrent API as its name indicates is a JAVA API that implements the Bittorrent Protocol ...

  7. 接口请求,上传byte数组byte[]数据异常,负数变正数/负数变63

    一.背景 最近项目中有个需求,就是需要把一个byte[]数组上传到服务端.但是我发现发送的byte数组和服务端接收的数组不一样,所有的正数在传递时正确,数组长度也没变化,但是负数变成了63或者负数全部 ...

  8. Java中如何正确的将byte[]数组转化为String类型?

    很多人在编程时,总是喜欢用一下方法将数组转为字符串:(a为byte数组) String s=a.toString(); 可是每次返回的时候,新手看来返回的结果是乱码,比如说我,写RSA算法时,没有注意 ...

  9. How to convert a byte to its binary string representation

    How to convert a byte to its binary string representation For example, the bits in a byte B are 1000 ...

随机推荐

  1. Hadoop分布式文件系统HDFS

    HDFS的探究: HDFS HDFS是 Hadoop Distribute File System的缩写,是谷歌GFS分布式文件系统的开源实现,Apache Hadoop的一个子项目,HDFS基于流数 ...

  2. Json1:使用gson解析、生成json

    Json解析: 1.json第三方解析包:json-lib.gson.jackson.fastjson等2.Google-gson只兼容jdk1.5版本以上:JSON-lib分别支持1.4和1.53. ...

  3. HTML中head与body标签

    一 head内常用标签 1.meta相关 #1.指定字符集 <meta charset="gbk"> #2.页面描述 <meta name="Descr ...

  4. 推荐算法 pd

    from django.db import connection select_sql = 'select * from model' datas = pd.read_sql(select_sql, ...

  5. eclipse 添加svn资源库卡死。长时间等待

    使用https://localhost:8443/svn/xx方式打入浏览器判断其服务器是否正常 如果正常通过,而eclipse新建库卡死时.可以等待一点时间看是否卡 问题依旧,考虑更改地址 主机名 ...

  6. mysql大纲

    一.概述 1.1 关系型数据.非关系型数据.半关系型数据 1.2 关系型数据库和非关系型数据库 1.3 发展史 二.MySQL组件和安装 三.数据库语言和主要概念 3.1 数据库语言 DML.DDL. ...

  7. python学习笔记_week24

    note 内容回顾: Model - 数据库操作 on_delete Query_set select_related 跨表数据一次性拿过来,不增加sql查询次数.帮助跨表,后面参数只能加连表字段 f ...

  8. php多图片上传。

    1. <form method="post" enctype="multipart/form-data" action='请求地址' > <i ...

  9. Jquery的一些基本操作

    /*获得TEXT.AREATEXT的值*/ var textval = $("#text_id").attr("value"); //或者 var textva ...

  10. vue下载和上传excle数据文件,解析excel文件数据并存在数据库中

    下载: VUE: window.open("xxxx/downloadOldTaskDataFile.do_", "_blank"); JAVA: /** * ...