0

Using C#.

I have a jpeg.

When I load it into a bitmap object its pixelformat is Format24bppRgb.

If I 'convert' the bitmap to Format16bppRgb565 the byte size of the file is larger.

I thought by reducing the pixelformat I would reduce the size of the file. In fact doing this has resutled in the file actually increasing is size.

Am A fundamentally wrong in this expectation?

This is my code (which is just a quick workaround)...

//imgConverted is at 24bpp

Bitmap bmp24bit = imgConverted.Clone(new Rectangle(0, 0, img.Width, img.Height), PixelFormat.Format16bppRgb565);

This is the code I use to convert bitmap to a byte array to compare my byte sizes:

    private byte[] ImageToByteArray(System.Drawing.Image imageIn)
    {
        byte[] data = null;
        try
        {
            using (MemoryStream ms = new MemoryStream())
            {
                imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                imageIn.Dispose();
                data = ms.ToArray();
            }
        }
        catch (Exception ex)
        {

        }
        finally
        {
            if (imageIn != null)
            {
                imageIn.Dispose();
            }
        }
        return data;
    }
4
  • How are you then saving the image afterwards, exactly? Perhaps you're specifying a higher quality level than the original? (That won't magically regain information, but it might cost more space to preserve the existing information...)
    – Jon Skeet
    May 29, 2014 at 5:52
  • @JonSkeet Hi, I do not save the bitmap to a file I instead save to a memorystream and read the byte count back from that. I will edit my question to show this code... May 29, 2014 at 5:54
  • 2
    Okay - I suspect it's a matter of supplying an ImageCodecInfo and EncoderParameters then. (I would recommend changing your code not to swallow exceptions, and not to dispose of a parameter, by the way.)
    – Jon Skeet
    May 29, 2014 at 6:05
  • @JonSkeet Thank you for the eductaion :) May 29, 2014 at 6:19

1 Answer 1

6

The PixelFormat enumeration is concerned with how many bits each pixel takes up when the image data is in uncompressed bitmap form (e.g. in-memory on your computer when you load an image file). File formats like JPEG and PNG do not have a comparable concept of bits-per-pixel because they're compressed formats that don't necessarily store each pixel in 24 bits of space on-disk.

As an aside, JPEG always uses 24 bits to store colour information (to the extent it does store colour, JPEG's algorithm is quite complicated actually), and PNG supports at least indexed colour, 24-bit RGB and 32-bit ARGB colour depth. PNG also supports 48-bit colour depth too.

Back on-topic: you're seeing increased JPEG file sizes because JPEG is a lossy algorithm that has the side-effect of introducing compression artefacts. While saving an original photo to a highly compressed JPEG image causes information loss, these artefacts actually add (unwanted) new details to the image, so when re-saving these new artefacts you're increasing the amount of "information" that needs to be stored, hence why the file-size increases. This will also cause the image to progressively lose quality each time it's re-saved - as compressed-bits that should be used to represent the original image are now being wasted to represent the artefacts that were introduced by a previous re-encoding of the JPEG image.

(There are ways to losslessly re-save JPEG images without running JPEG's algorithm again, but most image editors nor GDI support them, search for "jpegtrans" and "jpegcrop" for more information).

If you want to reduce the file-size of JPEG images then use the "JPEG Quality" parameter, which is a special feature unique to the JPEG algorithm which allows subjective "quality" to be modified:

This is documented here: http://msdn.microsoft.com/en-us/library/bb882583(v=vs.110).aspx

Copy & paste:

private void VaryQualityLevel()
{
// Get a bitmap.
Bitmap bmp1 = new Bitmap(@"c:\TestPhoto.jpg");
ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);

// Create an Encoder object based on the GUID 
// for the Quality parameter category.
System.Drawing.Imaging.Encoder myEncoder =
    System.Drawing.Imaging.Encoder.Quality;

// Create an EncoderParameters object. 
// An EncoderParameters object has an array of EncoderParameter 
// objects. In this case, there is only one 
// EncoderParameter object in the array.
EncoderParameters myEncoderParameters = new EncoderParameters(1);

EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"c:\TestPhotoQualityFifty.jpg", jgpEncoder, myEncoderParameters);

myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"c:\TestPhotoQualityHundred.jpg", jgpEncoder, myEncoderParameters);

// Save the bitmap as a JPG file with zero quality level compression.
myEncoderParameter = new EncoderParameter(myEncoder, 0L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"c:\TestPhotoQualityZero.jpg", jgpEncoder, myEncoderParameters);

}


private ImageCodecInfo GetEncoder(ImageFormat format)
{

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

foreach (ImageCodecInfo codec in codecs)
{
    if (codec.FormatID == format.Guid)
    {
        return codec;
    }
}
return null;
}
1
  • 1
    Hi, Thanks for the information. This explains everything and stops me going nuts :). I already use the above code. I was just looking at everything to decrease the bytes being uploaded and rendered to a browser and thus increasing FPS.. May 29, 2014 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.