<转载>byte[]、bitmap和stream之间的转换

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