一、二进制转换成图片

MemoryStream ms = new MemoryStream(bytes);
ms.Position = ;
Image img = Image.FromStream(ms);
ms.Close();
this.pictureBox1.Image

二、C#中byte[]与string的转换代码

.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);

三、C# Stream 和 byte[] 之间的转换

.将 Stream 转成 byte[]
public byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, , bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(, 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, , bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(, 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, , bytes.Length);
fileStream.Close();
// 把 byte[] 转换成 Stream
Stream stream = new MemoryStream(bytes);
return stream;
}

六、Bitmap 转化为 Byte[] 

Bitmap BitReturn = new Bitmap();
byte[] bReturn = null;
MemoryStream ms = new MemoryStream();
BitReturn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
bReturn = ms.GetBuffer();

转自:http://www.jb51.net/article/54573.htm

C#实现Stream与byte[]之间的转换实例教程的更多相关文章

  1. C# Stream 和 byte[] 之间的转换

    一. 二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream( ...

  2. Stream 和 byte[] 之间的转换

    Stream 和 byte[] 之间的转换 一. 二进制转换成图片 ? 1 2 3 4 5 MemoryStream ms = new MemoryStream(bytes); ms.Position ...

  3. C# Stream 和 byte[] 之间的转换(文件流的应用)

    一. 二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream( ...

  4. 将String转化成Stream,将Stream转换成String, C# Stream 和 byte[] 之间的转换(文件流的应用)

    static void Main( string[] args ) { string str = "Testing 1-2-3"; //convert string 2 strea ...

  5. C#下载文件,Stream 和 byte[] 之间的转换

    stream byte 等各类转换 http://www.cnblogs.com/warioland/archive/2012/03/06/2381355.html using (System.Net ...

  6. C# 之 Stream 和 byte[] 的相关转换

    1.二进制转换为图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream(m ...

  7. Drawable、Bitmap、byte[]之间的转换

    android在处理一写图片资源的时候,会进行一些类型的转换: 1 Drawable → Bitmap 的简单方法 ((BitmapDrawable)res.getDrawable(R.drawabl ...

  8. C#--整型与字节数组byte[]之间的转换

    using System; int  i = 123;byte [] intBuff = BitConverter.GetBytes(i);     // 将 int 转换成字节数组lob.Write ...

  9. 字符串与byte[]之间的转换

    一.  编码 同一个字符在不同的编码下会被编成不同长度的编码,比如: ACSII,每个字符对应一个字节,实际上只使用了7位,从00h-7Fh.只能表达128个字符. GB2312,中文的一种编码,每个 ...

随机推荐

  1. [软件工程]团队介绍&学长采访

    项目 内容 这个作业属于哪个课程 2019春季计算机学院软件工程(罗杰) 这个作业的要求在哪里 第一次团队作业 - 采访! 我们在这个课程的目标是 团队开发,合作学习 1.团队介绍 岗位 人员& ...

  2. LCT小小结

    模板题P3690 基础题P3203[HNOI2010]弹飞绵羊 \(access\)是搞出一条端点为\(x,y\)的路径 , 且维护的是实子树的信息 . 由于题目比较简单 , \(access\)时还 ...

  3. 【算法笔记】B1028 人口普查

    1028 人口普查 (20 分) 某城镇进行人口普查,得到了全体居民的生日.现请你写个程序,找出镇上最年长和最年轻的人. 这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过 2 ...

  4. bzoj3261: 最大异或和 (可持久化trie树)

    题目链接 题解 看到异或和最大就应该想到01 trie树 我们记\(S_i\)为前i项的异或和 那么我们的目的是最大化\(S_n\)^\(x\)^\(S_{j-1}\) \((l <= j &l ...

  5. HDU 6357.Hills And Valleys-动态规划(区间翻转l,r找最长非递减子序列)

    题意:给一串由n个数字组成的字符串,选择其中一个区间进行翻转,要求翻转后该字符串的最长非降子序列长度最长,输出这个最长非降子序列的长度以及翻转的区间的左右端点 #include<bits/std ...

  6. Poj:1064 : :Cable master (假定一个解并判断是否可行)(二分搜索答案)

    Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The J ...

  7. mock static方法

    <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mock ...

  8. [源代码]List的增加与删除

    // Removes the element at the given index. The size of the list is // decreased by one. // public vo ...

  9. 如何在新导入的python项目中一次性生成依赖的第三方库

    requirements.txt用来记录项目所有的依赖包和版本号,只需要一个简单的pip命令就能完成. pip freeze >requirements.txt 然后就可以用 pip insta ...

  10. html自定义垂直导航菜单

    html自定义垂直导航菜单(目前只支持上级+下级两级菜单) 由于工作的需要,昨天花了三个多小时的事件整理了一份关于垂直导航二级菜单,可以通过js配置的方式初始化菜单box(测试环境:chrome 49 ...