How to convert image to byte array

前端 未结 12 1650
悲哀的现实
悲哀的现实 2020-11-22 09:52

Can anybody suggest how I can convert an image to a byte array and vice versa?

I\'m developing a WPF application and using a stream reader.

12条回答
  •  暖寄归人
    2020-11-22 10:24

    Here's what I'm currently using. Some of the other techniques I've tried have been non-optimal because they changed the bit depth of the pixels (24-bit vs. 32-bit) or ignored the image's resolution (dpi).

      // ImageConverter object used to convert byte arrays containing JPEG or PNG file images into 
      //  Bitmap objects. This is static and only gets instantiated once.
      private static readonly ImageConverter _imageConverter = new ImageConverter();
    

    Image to byte array:

      /// 
      /// Method to "convert" an Image object into a byte array, formatted in PNG file format, which 
      /// provides lossless compression. This can be used together with the GetImageFromByteArray() 
      /// method to provide a kind of serialization / deserialization. 
      /// 
      /// Image object, must be convertable to PNG format
      /// byte array image of a PNG file containing the image
      public static byte[] CopyImageToByteArray(Image theImage)
      {
         using (MemoryStream memoryStream = new MemoryStream())
         {
            theImage.Save(memoryStream, ImageFormat.Png);
            return memoryStream.ToArray();
         }
      }
    

    Byte array to Image:

      /// 
      /// Method that uses the ImageConverter object in .Net Framework to convert a byte array, 
      /// presumably containing a JPEG or PNG file image, into a Bitmap object, which can also be 
      /// used as an Image object.
      /// 
      /// byte array containing JPEG or PNG file image or similar
      /// Bitmap object if it works, else exception is thrown
      public static Bitmap GetImageFromByteArray(byte[] byteArray)
      {
         Bitmap bm = (Bitmap)_imageConverter.ConvertFrom(byteArray);
    
         if (bm != null && (bm.HorizontalResolution != (int)bm.HorizontalResolution ||
                            bm.VerticalResolution != (int)bm.VerticalResolution))
         {
            // Correct a strange glitch that has been observed in the test program when converting 
            //  from a PNG file image created by CopyImageToByteArray() - the dpi value "drifts" 
            //  slightly away from the nominal integer value
            bm.SetResolution((int)(bm.HorizontalResolution + 0.5f), 
                             (int)(bm.VerticalResolution + 0.5f));
         }
    
         return bm;
      }
    

    Edit: To get the Image from a jpg or png file you should read the file into a byte array using File.ReadAllBytes():

     Bitmap newBitmap = GetImageFromByteArray(File.ReadAllBytes(fileName));
    

    This avoids problems related to Bitmap wanting its source stream to be kept open, and some suggested workarounds to that problem that result in the source file being kept locked.

提交回复
热议问题