c# Bitmap byte[] Stream 文件相互转换

本文提供了将图片与字节流相互转换的方法,包括如何将图片转换为byte数组以及如何将byte数组还原为图片。此外,还介绍了Stream与byte数组、文件之间的转换技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://blog.youkuaiyun.com/wangyue4/article/details/6819102




[csharp]  view plain copy print ?
  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, 0, bytes.Length);  
  58.   
  59.     // 设置当前流的位置为流的开始  
  60.     stream.Seek(0, 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. /* - - - - - - - - - - - - - - - - - - - - - - - -  
  75.  * Stream 和 文件之间的转换 
  76.  * - - - - - - - - - - - - - - - - - - - - - - - */  
  77. /// <summary>  
  78. /// 将 Stream 写入文件  
  79. /// </summary>  
  80. public void StreamToFile(Stream stream,string fileName)  
  81. {  
  82.     // 把 Stream 转换成 byte[]  
  83.     byte[] bytes = new byte[stream.Length];  
  84.     stream.Read(bytes, 0, bytes.Length);  
  85.     // 设置当前流的位置为流的开始  
  86.     stream.Seek(0, SeekOrigin.Begin);  
  87.   
  88.     // 把 byte[] 写入文件  
  89.     FileStream fs = new FileStream(fileName, FileMode.Create);  
  90.     BinaryWriter bw = new BinaryWriter(fs);  
  91.     bw.Write(bytes);  
  92.     bw.Close();  
  93.     fs.Close();  
  94. }  
  95.   
  96. /// <summary>  
  97. /// 从文件读取 Stream  
  98. /// </summary>  
  99. public Stream FileToStream(string fileName)  
  100. {              
  101.     // 打开文件  
  102.     FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);  
  103.     // 读取文件的 byte[]  
  104.     byte[] bytes = new byte[fileStream.Length];  
  105.     fileStream.Read(bytes, 0, bytes.Length);  
  106.     fileStream.Close();  
  107.     // 把 byte[] 转换成 Stream  
  108.     Stream stream = new MemoryStream(bytes);  
  109.     return stream;  
  110. }   

[csharp]  view plain copy print ?
  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, 0, bytes.Length);  
  58.   
  59.     // 设置当前流的位置为流的开始  
  60.     stream.Seek(0, 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. /* - - - - - - - - - - - - - - - - - - - - - - - -  
  75.  * Stream 和 文件之间的转换 
  76.  * - - - - - - - - - - - - - - - - - - - - - - - */  
  77. /// <summary>  
  78. /// 将 Stream 写入文件  
  79. /// </summary>  
  80. public void StreamToFile(Stream stream,string fileName)  
  81. {  
  82.     // 把 Stream 转换成 byte[]  
  83.     byte[] bytes = new byte[stream.Length];  
  84.     stream.Read(bytes, 0, bytes.Length);  
  85.     // 设置当前流的位置为流的开始  
  86.     stream.Seek(0, SeekOrigin.Begin);  
  87.   
  88.     // 把 byte[] 写入文件  
  89.     FileStream fs = new FileStream(fileName, FileMode.Create);  
  90.     BinaryWriter bw = new BinaryWriter(fs);  
  91.     bw.Write(bytes);  
  92.     bw.Close();  
  93.     fs.Close();  
  94. }  
  95.   
  96. /// <summary>  
  97. /// 从文件读取 Stream  
  98. /// </summary>  
  99. public Stream FileToStream(string fileName)  
  100. {              
  101.     // 打开文件  
  102.     FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);  
  103.     // 读取文件的 byte[]  
  104.     byte[] bytes = new byte[fileStream.Length];  
  105.     fileStream.Read(bytes, 0, bytes.Length);  
  106.     fileStream.Close();  
  107.     // 把 byte[] 转换成 Stream  
  108.     Stream stream = new MemoryStream(bytes);  
  109.     return stream;  
  110. }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值