SoFunction
Updated on 2025-03-07

Method for C# to process JPEG header information

Someone once gave me a jpg, which is 1024*1024 size, with a file size of 99kb, and made a pyramid. The image sizes of each layer are as follows: unit kb

The jpg pyramid I generated is for any processing

height256    46.2kb

height512    162kb

height1024   557kb

Photoshop generated jpg pyramid is processed for any processing

height256    48kb

height512    90kb

height1024   163kb

It can be seen that the size difference of this image is too big, but I can't figure it out. Finally, after reading the above blog post, I took a look at the reason by obtaining metadata:

Get metadata function

Copy the codeThe code is as follows:

public void GetProperty()

  {

    Bitmap myImage1024 = new Bitmap(@"E:\myjpg\");

    foreach (PropertyItem property in )

    {

      StringBuilder sb=new StringBuilder();

      Byte[] sbbyte = (Byte[]);

      ("ID:{0},Length:{1},Type:{2};\n",

(), (), (),);

      (sb);

     }

}

I found that the metadata of the jpg I generated and the original image are different

height1024:

ID:771,Length:1,Type:1; Header information Tag explanation: 303   PropertyTagSRGBRenderingIntent

ID:769,Length:8,Type:5; Header information Tag explanation: 301   PropertyTagGamma

ID:20752,Length:1,Type:1; Header information Tag explanation: 5110   PropertyTagPixelUnit resolution

ID:20753,Length:4,Type:4; Header information Tag explanation: 5111  PropertyTagPixelPerUnitX

ID:20754,Length:4,Type:4; Header information Tag explanation: 5112   PropertyTagPixelPerUnitY

 

height:

ID: 20625,Length:128,Type:3;Head information Tag explanation: 5091  PropertyTagChrominanceTable

ID:20624,Length:128,Type:3; Header information Tag explanation: 5090   PropertyTagLuminanceTable

So that's it, so we know why jpg is different in size. Well, I modified the method of generating image pyramids: remove their header information before saving the pyramids. Later, I was surprised to find that the finest layer of the image pyramid I generated was the same size as the original image! .

How to remove image metadata:

Copy the codeThe code is as follows:

public void RemoveProperty()

{

   Bitmap myImage1024 = new Bitmap(@"E:\myjpg\");         

   foreach (PropertyItem property in )

   {

       ();

   }

   (@"E:\myjpg\");

}