c# Bitmap byte[] Stream 文件相互转换
- //byte[] 转图片
- publicstatic Bitmap BytesToBitmap(byte[] Bytes)
- {
- MemoryStream stream = null;
- try
- {
- stream = new MemoryStream(Bytes);
- returnnew Bitmap((Image)new Bitmap(stream));
- }
- catch (ArgumentNullException ex)
- {
- throw ex;
- }
- catch (ArgumentException ex)
- {
- throw ex;
- }
- finally
- {
- stream.Close();
- }
- }
- //图片转byte[]
- publicstaticbyte[] BitmapToBytes(Bitmap Bitmap)
- {
- MemoryStream ms = null;
- try
- {
- ms = new MemoryStream();
- Bitmap.Save(ms, Bitmap.RawFormat);
- byte[] byteImage = new Byte[ms.Length];
- byteImage = ms.ToArray();
- return byteImage;
- }
- catch (ArgumentNullException ex)
- {
- throw ex;
- }
- finally
- {
- ms.Close();
- }
- }
- }
- =====================
- * Stream 和 byte[] 之间的转换
- * - - - - - - - - - - - - - - - - - - - - - - - */
- /// <summary>
- /// 将 Stream 转成 byte[]
- /// </summary>
- publicbyte[] StreamToBytes(Stream stream)
- {
- byte[] bytes = newbyte[stream.Length];
- stream.Read(bytes, 0, bytes.Length);
- // 设置当前流的位置为流的开始
- stream.Seek(0, SeekOrigin.Begin);
- return bytes;
- }
- /// <summary>
- /// 将 byte[] 转成 Stream
- /// </summary>
- public Stream BytesToStream(byte[] bytes)
- {
- Stream stream = new MemoryStream(bytes);
- return stream;
- }
- /* - - - - - - - - - - - - - - - - - - - - - - - -
- * Stream 和 文件之间的转换
- * - - - - - - - - - - - - - - - - - - - - - - - */
- /// <summary>
- /// 将 Stream 写入文件
- /// </summary>
- publicvoid StreamToFile(Stream stream,string fileName)
- {
- // 把 Stream 转换成 byte[]
- byte[] bytes = newbyte[stream.Length];
- stream.Read(bytes, 0, bytes.Length);
- // 设置当前流的位置为流的开始
- stream.Seek(0, SeekOrigin.Begin);
- // 把 byte[] 写入文件
- FileStream fs = new FileStream(fileName, FileMode.Create);
- BinaryWriter bw = new BinaryWriter(fs);
- bw.Write(bytes);
- bw.Close();
- fs.Close();
- }
- /// <summary>
- /// 从文件读取 Stream
- /// </summary>
- public Stream FileToStream(string fileName)
- {
- // 打开文件
- FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
- // 读取文件的 byte[]
- byte[] bytes = newbyte[fileStream.Length];
- fileStream.Read(bytes, 0, bytes.Length);
- fileStream.Close();
- // 把 byte[] 转换成 Stream
- Stream stream = new MemoryStream(bytes);
- return stream;
- }
c# Bitmap byte[] Stream 文件相互转换的更多相关文章
- BufferHelp byte[] Stream string FileStream Image Bitmap
/******* * *** ***** ** ** * * * * * * * * ***** * * * * * * * * * * * * * * * ******* *** * ***** * ...
- byte[] Base64 Stream 之间相互转换
图片 base64转byte[] /// <summary> /// 保存base64图片,返回阿里云地址 /// </summary> /// <param name= ...
- 【转】Drawable /Bitmap、String/InputStream、Bitmap/byte[]
原文:http://wuxiaolong.me/2015/08/10/Drawable-to-Bitmap/ Drawable互转Bitmap Drawable转Bitmap 1234 Resourc ...
- 【.Net】Byte,Stream,File的转换
引言 文件的传输和读写通常都离不开Byte,Stream,File这个类,这里我简单封装一下,方便使用. 帮助类 public static class FileHelper { / ...
- Android中Bitmap,byte[],Drawable相互转化
一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...
- 【Android】[转] Android中Bitmap,byte[],Drawable相互转化
一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...
- Android中如何将Bitmap byte裸数据转换成Bitmap图片int数据
Android中如何将Bitmap byte裸数据转换成Bitmap图片int数据 2014-06-11 10:45:14 阅读375次 我们在JNI中处理得到的BMP图片Raw数据,我们应该如何 ...
- Bitmap byte[] InputStream Drawable 互转
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStrea ...
- byte转文件流 下载到本地
此方法将byte类型文件转为文件流保存到本地 byte 经过BASE64Decoder 进行编码之后的类型 所以需要解码 防止出现乱码及文件损毁 /** * byte 转文件 下载到本地 * @par ...
随机推荐
- Python-str-操作-6
#字符串的索引与切片 s = 'ABCDLSESRF' #索引 s1 = s[0] print(s1) s2 = s[2] print(s2) s3 = s[-1] print(s3) s4 = s[ ...
- 《Linux内核分析》第五周学习笔记
<Linux内核分析>第五周学习笔记 扒开系统调用的三层皮(下) 郭垚 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.c ...
- Oracle 转移符问题
注:select '''' from dual; --输出一个单引号 select '''''' from dual; --输出两个单引号 select '''''''' ...
- 微信小程序navigator
如果是小程序自身页面的跳转 <navigator open-type="navigate" target="self" url="target ...
- vue props 用法(转载)
前面的话 组件接受的选项大部分与Vue实例一样,而选项props是组件中非常重要的一个选项.在 Vue 中,父子组件的关系可以总结为 props down, events up.父组件通过 props ...
- PAT 1046 划拳
https://pintia.cn/problem-sets/994805260223102976/problems/994805277847568384 划拳是古老中国酒文化的一个有趣的组成部分.酒 ...
- C#中byte[] 与string相互转化问题
using System; using System.IO; using System.Security.Cryptography; namespace ShareX.UploadersLib.Oth ...
- WebLogic: 内存溢出
每次部署完项目,重启Weblogic服务,总是遇到java.lang.OutOfMemoryError: PermGen space,其实物理内存也是够的,当时的解决方法就是多重启几次Weblogic ...
- [转帖]git、github、gitlab之间的关系
Git - 版本控制工具 Github - 一个网站,提供给用户空间创建git仓储,保存用户的一些数据文档或者代码等 GitLab - 基于Git的项目管理软件 Git分布式版本控制系统 Git是一款 ...
- zabbix 使用问题两个--中文乱码,以及监控ESXi下的虚拟机
1. 中文乱码 最开始中文显现 长方形不显示文字.解决办法: c:\windows\fonts 下面复制 楷体的字体(字体随意看自己喜欢) 文件名一般为: simkai.ttf 2.将simkai.t ...