1. //byte[] 转图片
  2. publicstatic Bitmap BytesToBitmap(byte[] Bytes)
  3. {
  4. MemoryStream stream = null;
  5. try
  6. {
  7. stream = new MemoryStream(Bytes);
  8. returnnew Bitmap((Image)new Bitmap(stream));
  9. }
  10. catch (ArgumentNullException ex)
  11. {
  12. throw ex;
  13. }
  14. catch (ArgumentException ex)
  15. {
  16. throw ex;
  17. }
  18. finally
  19. {
  20. stream.Close();
  21. }
  22. }
  23. //图片转byte[]
  24. publicstaticbyte[] BitmapToBytes(Bitmap Bitmap)
  25. {
  26. MemoryStream ms = null;
  27. try
  28. {
  29. ms = new MemoryStream();
  30. Bitmap.Save(ms, Bitmap.RawFormat);
  31. byte[] byteImage = new Byte[ms.Length];
  32. byteImage = ms.ToArray();
  33. return byteImage;
  34. }
  35. catch (ArgumentNullException ex)
  36. {
  37. throw ex;
  38. }
  39. finally
  40. {
  41. ms.Close();
  42. }
  43. }
  44. }
  45. =====================
  46. * Stream 和 byte[] 之间的转换
  47. * - - - - - - - - - - - - - - - - - - - - - - - */
  48. /// <summary>
  49. /// 将 Stream 转成 byte[]
  50. /// </summary>
  51. publicbyte[] StreamToBytes(Stream stream)
  52. {
  53. byte[] bytes = newbyte[stream.Length];
  54. stream.Read(bytes, 0, bytes.Length);
  55. // 设置当前流的位置为流的开始
  56. stream.Seek(0, SeekOrigin.Begin);
  57. return bytes;
  58. }
  59. /// <summary>
  60. /// 将 byte[] 转成 Stream
  61. /// </summary>
  62. public Stream BytesToStream(byte[] bytes)
  63. {
  64. Stream stream = new MemoryStream(bytes);
  65. return stream;
  66. }
  67. /* - - - - - - - - - - - - - - - - - - - - - - - -
  68. * Stream 和 文件之间的转换
  69. * - - - - - - - - - - - - - - - - - - - - - - - */
  70. /// <summary>
  71. /// 将 Stream 写入文件
  72. /// </summary>
  73. publicvoid StreamToFile(Stream stream,string fileName)
  74. {
  75. // 把 Stream 转换成 byte[]
  76. byte[] bytes = newbyte[stream.Length];
  77. stream.Read(bytes, 0, bytes.Length);
  78. // 设置当前流的位置为流的开始
  79. stream.Seek(0, SeekOrigin.Begin);
  80. // 把 byte[] 写入文件
  81. FileStream fs = new FileStream(fileName, FileMode.Create);
  82. BinaryWriter bw = new BinaryWriter(fs);
  83. bw.Write(bytes);
  84. bw.Close();
  85. fs.Close();
  86. }
  87. /// <summary>
  88. /// 从文件读取 Stream
  89. /// </summary>
  90. public Stream FileToStream(string fileName)
  91. {
  92. // 打开文件
  93. FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
  94. // 读取文件的 byte[]
  95. byte[] bytes = newbyte[fileStream.Length];
  96. fileStream.Read(bytes, 0, bytes.Length);
  97. fileStream.Close();
  98. // 把 byte[] 转换成 Stream
  99. Stream stream = new MemoryStream(bytes);
  100. return stream;
  101. }

c# Bitmap byte[] Stream 文件相互转换的更多相关文章

  1. BufferHelp byte[] Stream string FileStream Image Bitmap

    /******* * *** ***** ** ** * * * * * * * * ***** * * * * * * * * * * * * * * * ******* *** * ***** * ...

  2. byte[] Base64 Stream 之间相互转换

    图片 base64转byte[] /// <summary> /// 保存base64图片,返回阿里云地址 /// </summary> /// <param name= ...

  3. 【转】Drawable /Bitmap、String/InputStream、Bitmap/byte[]

    原文:http://wuxiaolong.me/2015/08/10/Drawable-to-Bitmap/ Drawable互转Bitmap Drawable转Bitmap 1234 Resourc ...

  4. 【.Net】Byte,Stream,File的转换

    引言      文件的传输和读写通常都离不开Byte,Stream,File这个类,这里我简单封装一下,方便使用. 帮助类     public static class FileHelper { / ...

  5. Android中Bitmap,byte[],Drawable相互转化

    一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...

  6. 【Android】[转] Android中Bitmap,byte[],Drawable相互转化

    一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...

  7. Android中如何将Bitmap byte裸数据转换成Bitmap图片int数据

    Android中如何将Bitmap byte裸数据转换成Bitmap图片int数据 2014-06-11 10:45:14   阅读375次 我们在JNI中处理得到的BMP图片Raw数据,我们应该如何 ...

  8. Bitmap byte[] InputStream Drawable 互转

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStrea ...

  9. byte转文件流 下载到本地

    此方法将byte类型文件转为文件流保存到本地 byte 经过BASE64Decoder 进行编码之后的类型 所以需要解码 防止出现乱码及文件损毁 /** * byte 转文件 下载到本地 * @par ...

随机推荐

  1. B. Heaters Div3

    链接 [http://codeforces.com/contest/1066/problem/B] 分析 具体看代码,贪就完事了 代码 #include<bits/stdc++.h> us ...

  2. Linux内核分析(第八周)

    进程的切换和系统的一般执行过程 一.进程切换的关键代码switch_to分析 1.进程调度与其时机分析 分类: 第一种分类 I/O-bound:频繁的进行I/O:会花很多时间等待I/O操作完成 CPU ...

  3. struts引入s标签

    <%@ taglib prefix="s" uri="/struts-tags"%>

  4. <<浪潮之巅>>阅读笔记一

    第一次的阅读就想读这本书的,却因为很多愿意一直拖到现在,因为听说这本书在李开复先生 的微博上有推荐,更是增加了我的阅读兴趣.可能是因为在网上找的电子版的原因,通篇阅读的速度很快,但是没有纸质数那种细嚼 ...

  5. Apache+php安装和配置 windows

    Apache+php安装和配置 windows Apache 安装 1.官网网址:http://httpd.apache.org/ 2.Download 3.点击链接Files for Microso ...

  6. HDU 2021 发工资咯:)

    http://acm.hdu.edu.cn/showproblem.php?pid=2021 Problem Description 作为杭电的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的 ...

  7. [转帖]TMD为你揭秘中国互联网下半场所有秘密

    https://www.iyiou.com/p/35099.html 李安说,<比利.林恩的中场战事>是“一个成长的故事”.中国互联网也行至中场,下半场如何走,成长的方向在哪里,成当下关键 ...

  8. Linux 更改root与home分区大小的方法总结

    1. 安装了CentOS7.5的虚拟机 但是发现里面的操作系统 home 分区占到了400g 根分区只有50g 认为不太好,所以要改一下. 2.方法. 好像是xfs的文件格式, 没法使用resize2 ...

  9. 如何隐藏Win7登录界面的administrator用户名恢复

    很多朋友一直在用着第三方的Windows7系统盘来装机,例如下载了Ghost格式的一些装机盘.在这些第三方系统中,很多家都是默认使用administrator 帐户自动登陆的. 从安全的角度来讲,这样 ...

  10. 配合es5.8的使用,升级sb版本到2.X,遇到一个问题

    问题:Failed to bind properties under 'spring.redis.jedis.pool.max-wait' to java.time.Duration: Propert ...