C# Stream 和 byte[] 之间的转换(文件流的应用)
一. 二进制转换成图片
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; }
C# Stream 和 byte[] 之间的转换(文件流的应用)的更多相关文章
- 将String转化成Stream,将Stream转换成String, C# Stream 和 byte[] 之间的转换(文件流的应用)
static void Main( string[] args ) { string str = "Testing 1-2-3"; //convert string 2 strea ...
- C# Stream 和 byte[] 之间的转换
一. 二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream( ...
- Stream 和 byte[] 之间的转换
Stream 和 byte[] 之间的转换 一. 二进制转换成图片 ? 1 2 3 4 5 MemoryStream ms = new MemoryStream(bytes); ms.Position ...
- C#实现Stream与byte[]之间的转换实例教程
一.二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream(m ...
- C#下载文件,Stream 和 byte[] 之间的转换
stream byte 等各类转换 http://www.cnblogs.com/warioland/archive/2012/03/06/2381355.html using (System.Net ...
- C# 之 Stream 和 byte[] 的相关转换
1.二进制转换为图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream(m ...
- C# 文件转byte数组,byte数组再转换文件
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- 字符串与byte[]之间的转换
一. 编码 同一个字符在不同的编码下会被编成不同长度的编码,比如: ACSII,每个字符对应一个字节,实际上只使用了7位,从00h-7Fh.只能表达128个字符. GB2312,中文的一种编码,每个 ...
- Drawable、Bitmap、byte[]之间的转换
android在处理一写图片资源的时候,会进行一些类型的转换: 1 Drawable → Bitmap 的简单方法 ((BitmapDrawable)res.getDrawable(R.drawabl ...
随机推荐
- codecademy-command line-inputoutput
What happens when you type this command? $ echo "Hello" Hello The echo command accepts the ...
- mysql删除二进制日志文件
一.RESET MASTER 这个语句可以验证首次配置主机备机是否成功.步骤如下: 1. 启动master和 slave,开启replication (即 复制) 注:replication (复制) ...
- cxGrid 增加序号 (非数据库绑定模式) (测试通过)
cxGrid 增加序号 (非数据库绑定模式) ----------------------------------- 1. 选在 adoQuery 控件 , 鼠标右键菜单中 选择 Fields Edi ...
- markdown 基本语法
代码块: ```console.log(1);```--- 标题: # h1## h2### h3 --- 粗斜体: *斜体***粗体*****粗斜体*** --- 强调:`强调` --- 链接:[百 ...
- 利用FFmpeg生成视频的缩略视频 v8.3
目前生成视频缩略图的工具大多数是生成静态的图片,为了解决这样的局限性,这 次春节期间搞了个利用 FFMpeg 能生成缩略动态视频的批处理. 把 Make_NxM_videos.bat LED_font ...
- Java for LeetCode 205 Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 服务器×××上的MSDTC不可用解决办法
MSDTC(分布式交易协调器),协调跨多个数据库.消息队列.文件系统等资源管理器的事务.该服务的进程名为Msdtc.exe,该进程调用系统Microsoft Personal Web Server和M ...
- xdebug安装
sudo apt-get install php-pearsudo apt-get install php5-devsudo pecl install xdebug 下载安装编译完后,在php.ini ...
- uva 489.Hangman Judge 解题报告
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...