Reduce image file size

S

shapper

Hello,

I am converting a PDF file pages to bitmaps and saving them as PNG's:

Bitmap image = file.GetPageImage(i, 144);
image = DrawingHelpers.Scale(image, 850D / image.Width,
InterpolationMode.HighQualityBicubic);
image.Save(String.Format("C:\\Test\\page{0}.png", i),
ImageFormat.Png);

I always get an image around 150KB for each page. I think that is
really a log.

When I try to save it as JPEG (ImageFormat.Jpeg) I am not able to open
the resulting file and in fact the file extension is still in PNG.

How can I make my files smaller?

I opened the resulting PNG files and saved them to JPG and got good
quality with around 60 KB.

Thanks,
Miguel
 
P

Peter Duniho

shapper said:
Hello,

I am converting a PDF file pages to bitmaps and saving them as PNG's:

Bitmap image = file.GetPageImage(i, 144);
image = DrawingHelpers.Scale(image, 850D / image.Width,
InterpolationMode.HighQualityBicubic);
image.Save(String.Format("C:\\Test\\page{0}.png", i),
ImageFormat.Png);

I always get an image around 150KB for each page. I think that is
really a log.

Whether that's "really a log" ("really a lot"?) depends entirely on how
many pixels are actually in the image. If you're talking about a 8.5x11
inch image at 144 dpi, I'd say 150KB isn't bad.
When I try to save it as JPEG (ImageFormat.Jpeg) I am not able to open
the resulting file and in fact the file extension is still in PNG.

Well, sure. If you tell the Save() method to write a file named
"C:\\Test\\page1.png", it's going to save a file with the PNG extension.
But, if the format you tell it to save is ImageFormat.Jpeg, then it
will save JPEG formatted data into that file with the PNG extension.

Some image reading code will handle this just fine. They just inspect
the file contents and try to interpret them as whatever formats they
know. Others will fail if the contents don't match the extension given.

Sounds like you're trying to look at the file with an example of the
latter. Regardless, IMHO it's always a good idea to use a file
extension that's actually appropriate for the contents of the file.
Things go so much more smoothly when you do.

So, if you want to save as JPEG, just fix the filename so that the
extension you give it is also the JPEG extension (e.g. "jpg" or "jpeg").
How can I make my files smaller?

Saving as PNG, you can't. Not from .NET. .NET doesn't provide control
over the various PNG compression options, so what you get is what you get.

You can save as JPEG, which even by default is likely to be smaller than
PNG (since JPEG is lossy and PNG is not). And in .NET, you also have
some control over the JPEG compression options (i.e. quality level from
0 to 100), which indirectly affects the file size as well. So if the
defaults don't work for you, you can adjust them as you see fit.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top