Reducing Bitmap Object size

Started by
7 comments, last by BeerNutts 11 years, 11 months ago
Hey, just wondering is there anyway of reducing the size of a bitmap object(space on the disk, not dimensions) before I save it to disk? I have a program which is accepting images as input, changing the RGB values of the image, and then outputting the new image, the problem is the outputted image always takes up more memory than the original image even though if I am not adjusting its dimensions in anyway.

Google searches typically turn up information regarding dimensions and not disk space. Any information on handling images in a different fashion or reducing the space of a bitmap object would be appreciated.
Advertisement
You are probably changing the bitmap's color depth (e.g. reading it as a RGB image (96 bits per pixel) and writing it back as a RGBA image (128 bits per pixel)). Did you check that? This is my best guess.

As for reducing file size in general, bitmaps are often highly compressible. You could compress the file before saving it to disk and decompress it before reading it. Bitmap doesn't offer much support in this direction, so you may have to use an external library to perform the compression. Or you could use PNG's instead which natively support compression.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Thanks for the reply Bacterius,

How would I check if I am writing it back as an RGBA image? I am creating new pixel colors using the Color.FromArgb() method, and saving the image using the Bitmap Class' save() method
Just check the bitmap's type in the file's properties (or check the file size - if the file size changes according to a 3:4 ratio it's almost certain this is what's happening). As for saving in the correct format, you want to change the bitmap's pixelformat before saving it (I assume - I've never used the BItmap class in C# but the terminology is standard across most languages):

http://msdn.microsoft.com/en-us/library/system.drawing.image.pixelformat.aspx

So for a 24-bit RGB image you will want the PixelFormat.Format24bppRgb one probably. My guess is the bitmap reverted to RGBA format as you wanted to set alpha channels (otherwise it shouldn't have changed).

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

You are right, the original image that was read came in the format of 24bppRgb, however the one being outputted is 32bppArgb. The problem is the original image is actually being read from a pdf file of the size ~110 kb, and the bitmap object that is being outputted is around 2mb!! obviously the problem isn't limited to the pixel format, however this a start in the right direction.

Thank you for your help.
The bitmap could be compressed, which would explain the huge size difference. The bitmap "specification" does allow for compression to take place (there's a field for it in the header but I've personally never seen it used). Perhaps the Bitmap class provides a wrapper or some sort of interface to do the same thing, but I wouldn't know that much :)

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

The pdf is obviously compressed. When you read the bmp off the PDF, you read it uncompressed, and then save it as such. In general, a RAW BMP, even using 24 bpp, will be width*height*3 bytes large. Using that, you will find the minimum size you will be output using 24bpp with no compression.

If you want compression, there's 2 options: #1, using BMP's built in compression technique, which is Run Length Encoding (RLE), and not the best (google it if you want). Or,
#2, use an external library to compress the image into another format (PNG, for example), and save that.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

There's also the option of using his own compression on a normal BMP file. Huffman coding would likely provide decent compression compared to RLE, and is not terribly difficult to program.

There's also the option of using his own compression on a normal BMP file. Huffman coding would likely provide decent compression compared to RLE, and is not terribly difficult to program.


But then it wouldn't be a BMP, it would be his own format. And he would need a special program to open it. I'm assuming he wants to keep it within the bounds of a popular image standard, thus, RLE in a BMP (which is supported in the BMP header), or using PNG (or some other format if he chooses)

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement