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

C# Byte[]、Image、Bitmap 之间的相互转换的更多相关文章

  1. Byte[]、Image、Bitmap 之间的相互转换

    原文:Byte[].Image.Bitmap 之间的相互转换 /// <summary>        /// 将图片Image转换成Byte[]        /// </summ ...

  2. android开发之Bitmap 、byte[] 、 Drawable之间的相互转换

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

  3. C# Image 、 byte[] 、Bitmap之间的转化

    一.Byte[] 转 System.Drawing.Bitmap public static Bitmap CreateBitmap(byte[] originalImageData, int ori ...

  4. byte[],bitmap,drawable之间的相互转换

    Byte[]转Bitmap BitmapFactory.decodeByteArray(data, 0, data.length); Bitmap转Byte[] ByteArrayOutputStre ...

  5. Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】

    package com.soai.imdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; ...

  6. Byte[]、Image、Bitmap_之间的相互转换

    1.将图片Image转换成Byte[] /// <summary>        /// 将图片Image转换成Byte[]        /// </summary>     ...

  7. Android中Bitmap对象和字节流之间的相互转换

    android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte   import java.io.B ...

  8. Android中Bitmap对象和字节流之间的相互转换(转)

    android 将图片内容解析成字节数组:将字节数组转换为ImageView可调用的Bitmap对象:图片缩放:把字节数组保存为一个文件:把Bitmap转Byte import java.io.Buf ...

  9. Python网络编程——主机字节序和网络字节序之间的相互转换

    If you ever need to write a low-level network application, it may be necessary to handle the low-lev ...

随机推荐

  1. Vue项目页面跳转时候的,浏览器窗口上方的进度条显示

    1.安装: cnpm install --save nprogress 2.在main.js中引入: import NProgress from 'nprogress' import 'nprogre ...

  2. MAC下是用brew安装Redis

    启动redis redis-server /etc/redis.conf 安装brew 在命令行里运行下面的命令,需要等待一段时间. ruby -e "$(curl -fsSL https: ...

  3. lsblk命令

    lsblk命令用于列出所有可用块设备的信息,而且还能显示他们之间的依赖关系,但是它不会列出RAM盘的信息.块设备有硬盘,闪存盘,cd-ROM等等.lsblk命令包含在util-linux-ng包中,现 ...

  4. python 正则表达式 -- IP地址验证

    p = re.compile("^((?:(2[0-4]\d)|(25[0-5])|([01]?\d\d?))\.){3}(?:(2[0-4]\d)|(255[0-5])|([01]?\d\ ...

  5. C++中的new、operator new与placement new

    转:http://www.cnblogs.com/luxiaoxun/archive/2012/08/10/2631812.html new/delete与operator new/operator ...

  6. WebSphere ILog JRules 域的介绍和定制

    WebSphere ILog JRules 域的介绍和定制 引言 随着企业业务的不断发展,越来越多的企业正经历着以下的情形: 企业需要对于业务系统的频繁变化做出及时的关注和响应,例如,竞争对手或经济环 ...

  7. 键盘 Input子系统

    应用层测试代码 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <li ...

  8. 115道Java经典面试题(面中率最高、最全)

    115道Java经典面试题(面中率最高.最全) Java是一个支持并发.基于类和面向对象的计算机编程语言.下面列出了面向对象软件开发的优点: 代码开发模块化,更易维护和修改. 代码复用. 增强代码的可 ...

  9. hbase1.4.0安装和使用

    jia下载地址:  http://mirrors.shuosc.org/apache/hbase/1.4.0/ 解压 tar -zxvf hbase-1.4.0-bin.tar.gz 修改环境变量 [ ...

  10. SpringBoot2 时间类型自动格式化 自动转换

    package com.archibladwitwicke.springboot2.chapter03.configurer; import com.archibladwitwicke.springb ...