惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
P
Proofpoint News Feed
Apple Machine Learning Research
Apple Machine Learning Research
WordPress大学
WordPress大学
博客园 - Franky
V
V2EX
爱范儿
爱范儿
J
Java Code Geeks
小众软件
小众软件
Last Week in AI
Last Week in AI
The Cloudflare Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
酷 壳 – CoolShell
酷 壳 – CoolShell
The Register - Security
The Register - Security
GbyAI
GbyAI
Vercel News
Vercel News
Y
Y Combinator Blog
腾讯CDC
F
Fortinet All Blogs
I
InfoQ
N
Netflix TechBlog - Medium
B
Blog RSS Feed
D
Docker
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
量子位
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
The GitHub Blog
The GitHub Blog
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
Blog — PlanetScale
Blog — PlanetScale
H
Help Net Security
云风的 BLOG
云风的 BLOG
A
About on SuperTechFans
Scott Helme
Scott Helme
T
Tor Project blog
U
Unit 42
Google Online Security Blog
Google Online Security Blog
PCI Perspectives
PCI Perspectives
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
NISL@THU
NISL@THU
D
Darknet – Hacking Tools, Hacker News & Cyber Security
aimingoo的专栏
aimingoo的专栏
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
Security Archives - TechRepublic
Security Archives - TechRepublic

博客园 - iQingHan

underscore-1.8.3-analysis.js 经典JS 12 extremely useful hacks for JavaScript IIS字体文件添加MIME映射 js类型判断的方法 基于html5 canvas 的客户端异步上传图片的插件,支持客户端压缩图片尺寸 Google Chrome 默认非安全端口列表 js判断类型的方法 博客园样式排版自定义 easyloader.js源代码分析 JQuery操作cookies jQuery插件的开发 jQuery 1.9 移除了 $.browser 的替代方法 Pretty Gmail GreasemonkeyScript jQuery之移除链接小插件 ASP.NET中关于验证控件和自定义弹出确认窗口的冲突问题 JQuery获取浏览器窗口的可视区域高度和宽度,滚动条高度 jQuery的选择器 园子里使用的jQuery.json
C#中二进制和流之间的各种相互转换
iQingHan · 2017-01-14 · via 博客园 - iQingHan

一. 二进制转换成图片间的转换

1

2

3

4

5

MemoryStream ms = new MemoryStream(bytes);

ms.Position = 0;

Image img = Image.FromStream(ms);

ms.Close();

this.pictureBox1.Image

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

1.

1

2

3

System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();

  byte[] inputBytes =converter.GetBytes(inputString);

  string inputString = converter.GetString(inputBytes);

2.

1

2

3

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[] 之间的转换

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

/// 将 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 写入文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

public void StreamToFile(Stream stream,string fileName)

{

    byte[] bytes = new byte[stream.Length];

    stream.Read(bytes, 0, bytes.Length);

    stream.Seek(0, SeekOrigin.Begin);

    FileStream fs = new FileStream(fileName, FileMode.Create);

    BinaryWriter bw = new BinaryWriter(fs);

    bw.Write(bytes);

    bw.Close();

    fs.Close();

}

五. 从文件读取 Stream

1

2

3

4

5

6

7

8

9

10

11

12

13

14

public Stream FileToStream(string fileName)

{            

    FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);

    byte[] bytes = new byte[fileStream.Length];

    fileStream.Read(bytes, 0, bytes.Length);

    fileStream.Close();

    Stream stream = new MemoryStream(bytes);

    return stream;

}

六Bitmap 转化为 Byte[] 

1

2

3

4

5

6

                Bitmap BitReturn = new Bitmap();

                byte[] bReturn = null;

                MemoryStream ms = new MemoryStream();

                BitReturn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

                bReturn = ms.GetBuffer();

posted @ 2017-01-14 14:30  iQingHan  阅读(1449)  评论()    收藏  举报