被一个小问题给恶心了一回。
留影,备查。
1 class BolbHelper 2 { 3 /// <summary> 4 /// 将图像转为二进制字节数组 5 /// </summary> 6 /// <param name="image"></param> 7 /// <returns></returns> 8 public static byte[] ImageToBinaryBytes(Image image) 9 { 10 byte[] result; 11 using (var ms = new MemoryStream()) 12 { 13 image.Save(ms, ImageFormat.Bmp); 14 using(var br = new BinaryReader(ms)) 15 { 16 ms.Seek(0, SeekOrigin.Begin); 17 result = br.ReadBytes((int)ms.Length); 18 br.Close(); 19 } 20 ms.Close(); 21 } 22 return result; 23 } 24 25 /// <summary> 26 /// 从二进制字节数组读取图像 27 /// </summary> 28 /// <param name="data"></param> 29 /// <returns></returns> 30 public static Image ImageFromBinaryBytes(byte[] data) 31 { 32 Image result; 33 using (var ms = new MemoryStream()) 34 { 35 using(var bw = new BinaryWriter(ms)) 36 { 37 bw.Write(data); 38 result = Image.FromStream(ms); 39 bw.Close(); 40 } 41 ms.Close(); 42 } 43 return result; 44 } 45 }