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

推荐订阅源

H
Hackread – Cybersecurity News, Data Breaches, AI and More
U
Unit 42
Vercel News
Vercel News
Martin Fowler
Martin Fowler
云风的 BLOG
云风的 BLOG
爱范儿
爱范儿
MongoDB | Blog
MongoDB | Blog
J
Java Code Geeks
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
C
Check Point Blog
N
Netflix TechBlog - Medium
Microsoft Azure Blog
Microsoft Azure Blog
aimingoo的专栏
aimingoo的专栏
博客园_首页
WordPress大学
WordPress大学
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
Last Week in AI
Last Week in AI
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
Jina AI
Jina AI
V
Visual Studio Blog
小众软件
小众软件

博客园 - 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)  评论()    收藏  举报