首页 > Windows开发 > 详细

C# byte[]与Image类型互相转换

时间:2021-02-20 11:58:34      阅读:29      评论:0      收藏:0      [点我收藏+]

1.将Image转换成byte[]

1         public byte[] ImageToByteArray(Image image)
2         {
3             MemoryStream imageStream = new MemoryStream();
4             image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
5             byte[] imageContent = new Byte[imageStream.Length];
6             imageStream.Position = 0;
7             imageStream.Read(imageContent, 0, (int)imageStream.Length);
8             return imageStream.ToArray();
9         }

2.将byte[]Image转换成Image

 1         public Image ByteToImage(byte[] byteImage)
 2         {
 3             MemoryStream stream = null;
 4             try
 5             {
 6                 stream = new MemoryStream(byteImage);
 7                 Image bitmapnew = ((Image)new Bitmap(stream));
 8                 return bitmapnew;
 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         }

 

C# byte[]与Image类型互相转换

原文:https://www.cnblogs.com/yellow3gold/p/14419427.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!