C#:文件、byte[]、Stream相互转换
一、byte[] 和 Stream
/// <summary>
/// byte[]转换成Stream
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
} /// <summary>
/// Stream转换成byte[]
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
stream.Seek(0, SeekOrigin.Begin); // 设置当前流的位置为流的开始
return bytes;
}
二、文件 和 Stream
/// <summary>
/// 从文件读取Stream
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public Stream FileToStream(string path)
{
FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); // 打开文件
byte[] bytes = new byte[fileStream.Length]; // 读取文件的byte[]
fileStream.Read(bytes, 0, bytes.Length);
fileStream.Close();
Stream stream = new MemoryStream(bytes); // 把byte[]转换成Stream
return stream;
} /// <summary>
/// 将Stream写入文件
/// </summary>
/// <param name="stream"></param>
/// <param name="path"></param>
public void StreamToFile(Stream stream, string path)
{
byte[] bytes = new byte[stream.Length]; // 把Stream转换成byte[]
stream.Read(bytes, 0, bytes.Length);
stream.Seek(0, SeekOrigin.Begin); // 设置当前流的位置为流的开始
FileStream fs = new FileStream(path, FileMode.Create); // 把byte[]写入文件
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(bytes);
bw.Close();
fs.Close();
}
转自:http://www.cnblogs.com/warioland/archive/2012/03/06/2381355.html
C#:文件、byte[]、Stream相互转换的更多相关文章
- IRandomAccessStream, IBuffer, Stream, byte[] 之间相互转换
/* * 用于实现 IRandomAccessStream, IBuffer, Stream, byte[] 之间相互转换的帮助类 */ using System;using System.IO;us ...
- JAVA中文件与Byte数组相互转换的方法
JAVA中文件与Byte数组相互转换的方法,如下: public class FileUtil { //将文件转换成Byte数组 public static byte[] getBytesByFile ...
- C# 流与文件(Stream & File & byte[])
原文:https://www.cnblogs.com/long-gengyun/archive/2010/03/28/1698681.html 文件概述 文件在操作时表现为流,即流是从一些输入中读取 ...
- Byte[]和Stream相互转换
C# Stream 和 byte[] 之间的转换 一. 二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = 0; Ima ...
- BufferHelp byte[] Stream string FileStream Image Bitmap
/******* * *** ***** ** ** * * * * * * * * ***** * * * * * * * * * * * * * * * ******* *** * ***** * ...
- 通过文件流stream下载文件
public ActionResult ShowLocalizedXML(int id) { string orderName = ""; string xmlString = G ...
- (一)一个工作任务引起的乱战——c#中结构体与byte[]间相互转换
一个工作任务涉及到c#与c++系统间的udp通信,处理了蛮长时间没有完成任务,但是期间接触到不少小知识点.本人是初接触c#,c++语言没有接触过.可能写的东西都很小儿科,暂且记录下来当工作日记把. 先 ...
- 【.Net】Byte,Stream,File的转换
引言 文件的传输和读写通常都离不开Byte,Stream,File这个类,这里我简单封装一下,方便使用. 帮助类 public static class FileHelper { / ...
- Serializable 序列化 The byte stream created is platform independent. So, the object serialized on one platform can be deserialized on a different platform.
Java 序列化接口Serializable详解 - 火星猿类 - 博客园 http://www.cnblogs.com/tomtiantao/p/6866083.html 深入理解JAVA序列化 - ...
随机推荐
- 部署OpenStack问题汇总(二)--openstack dashboard 问题解决方案
在打开dashboard的时候报错: LocationParseError at /admin/ (LocationParseError(...), 'Failed to parse: Failed ...
- Oracle SQL Developer保持数据库连接的方法
一.概述 从navicat切到pl/sql developer,但是发现个bug,因为本地客户端pl/sql developer的字符集 和数据库服务器的字符集不一致,导致一个很奇葩的东西. 本来我有 ...
- 【咸鱼教程】BitmapLabel位图字体使用
引擎版本3.2.6 教程目录一 为什么要使用位图字体二 如何使用位图字体2.1 TextureMerger制作位图字体2.2 exml中使用位图字体三 Demo源码 一 为什么要使用位图字体egre ...
- flask BytesIO() 多个文件打包下载 zipfile
使用zipfile模块可以将多个文件打包成zip文件进行下载,但是常规的操作方式会在服务器磁盘上生成一个zip文件占用磁盘空间. 后引入BytesIO将文件写入到内存中然后下载: def dl_pla ...
- 利用 background 和 filter 模糊指定区域
背景知识:background-size: cover;,background-attachment:fixed;,filter:blur() 难题: 通常,我们会通过filter:blur()去实现 ...
- POJ 1815 - Friendship - [拆点最大流求最小点割集][暴力枚举求升序割点] - [Dinic算法模板 - 邻接矩阵型]
妖怪题目,做到现在:2017/8/19 - 1:41…… 不过想想还是值得的,至少邻接矩阵型的Dinic算法模板get√ 题目链接:http://poj.org/problem?id=1815 Tim ...
- jquery on事件jquery on实现绑定多个事件
API上jquery on介绍 on(events,[selector],[data],fn) 概述 在选择元素上绑定一个或多个事件的事件处理函数. on()方法绑定事件处理程序到当前选定的jQuer ...
- 快速排序之python
快速排序( Quick sort) 快速排序的基本思想:通过一趟排序将待排记录分隔成独立的两部分,其中一部分记录的关键字均比另一部分的关键字小,则可分别对这两部分记录继续进行递归排序,以达到整个序列有 ...
- JIRA licence and vulnarability,jenkins,devops
http://blog.itpub.net/13651903/viewspace-1079918/ http://www.freebuf.com/articles/web/34051.html JIR ...
- dp——01背包
今天学习了01背包不算是复习吧,发现完全不会状态之间的转移如此让我捉摸不透尽管很简单但本人觉得还是很难,奇怪地拐点也很难被发现.知道01背包二维的话是很慢的,然后就是非得先打二维毕竟一维是根据二维的想 ...