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. Dubbo原理和源码解析之“微内核+插件”机制

    github新增仓库 "dubbo-read"(点此查看),集合所有<Dubbo原理和源码解析>系列文章,后续将继续补充该系列,同时将针对Dubbo所做的功能扩展也进行 ...

  2. Pair Project —— Elevator Scheduler

    结对编程人员 12061153 刘丽萍 12061154 冯飘飘 说明结对编程的优点和缺点. 结对编程的优点: 以前都是自己一个人编程,对于相互结对或者团队编程都没有接触过.而自己在写代码时不可避免的 ...

  3. linux内核分析第七次实验

    实验: rm menu -rf git clone https://github.com/megnning/menu.git cd menu ls mv test_exec.c test.c vi t ...

  4. Linux内核分析— —扒开系统调用的三层皮(下)

    课程主要内容有三点: 在MenuOS中通过添加代码增加自定义的系统调用命令 使用gdb跟踪调试内核 简单分析system_call代码了解系统调用在内核代码中的处理过程 实验——分析system_ca ...

  5. GIthub地址

    https://github.com/cuibaoxue/Text1

  6. R和python语言如何求平均值,中位数和众数

    均值是通过取数值的总和并除以数据序列中的值的数量来计算. R语言平均值公式: mean(x, trim = 0, na.rm = FALSE, ...)#x - 是输入向量.trim - 用于从排序的 ...

  7. 开源RabbitMQ操作组件

    开源RabbitMQ操作组件 对于目前大多的.NET项目,其实使用的技术栈都是差不多,估计现在很少用控件开发项目的了,毕竟一大堆问题.对.NET的项目,目前比较适合的架构ASP.NET MVC,ASP ...

  8. TensorFlow中的优化算法

    搭建好网络后,常使用梯度下降类优化算法进行模型参数求解,模型越复杂我们在训练神经网络的过程上花的时间就越多,为了解决这一问题,我们就需要找一些优化算法来提高训练速度,TF的tf.train模块中提供了 ...

  9. awk、sed、grep三大shell文本处理工具之awk的应用

    awk 1.是什么 是一个编程语言.支持变量.数组.函数.流程控制(if...else/for/while) 单行程序语言. 2.工作流程 读取file.标准输入.管道给的数据,从第一行开始读取,逐行 ...

  10. Lodop打印控件里SET_PRINT_STYLE和SET_PRINT_STYLEA

    LODOP.SET_PRINT_STYLE 对该语句后面的打印项样式设置效果.LODOP.SET_PRINT_STYLEA 针对第一个参数设置的打印项样式设置效果.这两个语句,作用范围不同. 在设置字 ...