• Advertisement
Sign in to follow this  

Reducing Bitmap Object size

This topic is 2076 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='bobbias' timestamp='1337097008' post='4940443']
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.
[/quote]

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) Edited by BeerNutts

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement