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 }
/********************************/
所有文章内容均属本人自己在日常的工作和学习过程中总结出的一些帮助自己学习和进步的知识点,难免会有不足之处。
仅供参考,如有疑问,谨请联系。
/*******************************/