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

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

 1         public Image ByteToImage(byte[] byteImage)
 2         {
 3             MemoryStream stream = null;
 4             try
 5             {
 6                 using (stream = new MemoryStream(byteImage))
 7                 {
 8                     Image bitmapnew = ((Image)new Bitmap(stream));
 9                     return bitmapnew;
10                 }
11             }
12             catch (ArgumentNullException ex)
13             {
14                 throw ex;
15             }
16             catch (ArgumentException ex)
17             {
18                 throw ex;
19             }
20         }

 

posted @ 2021-02-20 10:11  新*  阅读(1147)  评论(0编辑  收藏  举报