Illuminati0x5B
Illuminati0x5B

Reputation: 602

Difference between reading an image using Emgu and reading it as a byte array

I have a .png file and I did the following two things

  1. Read the file as a byte array

    byte[] arr = File.ReadAllBytes(Filename)
    
  2. Read it using Emgu, read the file into Image and then converted the Bitmap to a byte array using the following.

    Image<Gray,Byte> Img = new Image<Gray,Byte>(Filename);
    byte[] arr = ImageToByte2(Img.Bitmap);
    
    
    public static byte[] ImageToByte2(Image img) 
    
    {
    
       using (MemoryStream stream = new MemoryStream())
    
       {
    
      img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
    
      return stream.ToArray();
    
       }
    
    }
    

I have a difference in length of the byte array. I don't understand why there is a difference. Please help.

Upvotes: 2

Views: 332

Answers (2)

Anders
Anders

Reputation: 600

When you use EMGU to generate the PNG byte array, you can't be sure that the compression level is the same as the PNG image you had in the first place.

There is an overload for the save method, where you can specify encoder parameters as well. If you adjust the compression level, maybe you can get the same byte length.

Upvotes: 1

RomCoo
RomCoo

Reputation: 1893

The first option reads all bytes of the file including the header, while the second one just reads the byte of the plain image.

For more info on the structure and the header of a png look here: https://en.wikipedia.org/wiki/Portable_Network_Graphics

Upvotes: 1

Related Questions