C#中图片类型与二进制流的转化

将二进制字节流转换为图片类型

 1     private Bitmap BytesToBitmap(byte[] Bytes)
 2     {
 3         MemoryStream stream = null;
 4         try
 5         {
 6             stream = new MemoryStream(Bytes);
 7             return new Bitmap((Image)new Bitmap(stream));
 8         }
 9         catch (ArgumentNullException ex)
10         {
11             throw ex;
12         }
13         catch (ArgumentException ex)
14         {
15             throw ex;
16         }
17         finally
18         {
19             stream.Close();
20         }
21     }

将图片类型转换为二进制字节流:

 1     private byte[] BitmapToBytes(Bitmap Bitmap)
 2     {
 3         MemoryStream ms = null;
 4         try
 5         {
 6             ms = new MemoryStream();
 7             Bitmap.Save(ms, ImageFormat.Gif);
 8             Bitmap.Save("C:/test.jpg", ImageFormat.Gif);
 9             byte[] byteImage = new Byte[ms.Length];
10             byteImage = ms.ToArray();
11             return byteImage;
12         }
13         catch (ArgumentNullException ex)
14         {
15             throw ex;
16         }
17         finally
18         {
19             ms.Close();
20         }
21     } 

 

posted @ 2012-09-15 23:24  Bart Li  阅读(916)  评论(0编辑  收藏  举报