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[ ...
- 解决AJAX session跨域失效
1.想实现的功能是登录时有个验证码,这个验证码后台提供,然后放在session中,前台把用户输入的验证码通过AJAX发给后台,后台把session中的验证码取出来然后比较不同,一样则通过. 问题出现在 ...
- 【软件工程】5.8 黑盒&白盒测试
代码链接:http://www.cnblogs.com/bobbywei/p/4469145.html#3174062 搭档博客:http://www.cnblogs.com/Roc201306114 ...
- PHP和JavaScript将字符串转换为数字string2int
在看廖雪峰的JavaScript教程时,里面有一个题就是利用reduce()将string转换为int,我看评论中贴出的方法,当时觉得挺意外了,以为他只用了一行代码,即下面这行代码 var str=& ...
- Activiti解析.bpmn文件获得User Task节点的CandidateUsers特性的值
参考文档: http://www.cnblogs.com/mingforyou/p/5351332.html http://blog.csdn.net/jackyrongvip/article/det ...
- Mybatis源码分析
MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以对配置和原生Map使用简 ...
- ubuntu安装steam
增加第三方自由库的软件支持 sudo add-apt-repository multiverse 增加更新支持包 sudo add-apt-repository multiverse 安装steam ...
- pandas缺失值处理
1.检查缺失值 为了更容易地检测缺失值(以及跨越不同的数组dtype),Pandas提供了isnull()和notnull()函数,它们也是Series和DataFrame对象的方法 - 示例1 im ...
- sql语句中日期相减的操作
select datediff(year, 开始日期,结束日期); --两日期间隔年select datediff(quarter, 开始日期,结束日期); --两日期间隔季select datedi ...
- codeforces500B
New Year Permutation CodeForces - 500B User ainta has a permutation p1, p2, ..., pn. As the New Year ...