views:

54

answers:

2

Hi,

I am writing a program that resizes pictures like this:

Image originalImage = Image.FromFile(pathToOriginalPicture);

Bitmap b = new Bitmap(newWidth, newHeight);
Graphics g = Graphics.FromImage(b);

g.DrawImage(originalImage, 0, 0, newWidth, newHeight);
g.Dispose();

b.Save(pathToOutputPicture, ImageFormat.Jpeg);

I tried to set:

newWidth = originalImage.Width;
newHeight = originalImage.Height;

The result was that the rezised picture file became ~900K while the original file was ~4M.

Why this is happening ? Is the quality of the original picture better than the resized one ? How?

I opened both pictures in Photoshop and I see that the original picture was 72ppi, while the resized one became 96ppi. Why is that ? Can I control this ?

Thanks a lot for your time !

A: 

Besides the format you need to set DPI, compression level settings etc. Check your Save function for overloads that will accept this type of input. See this documentation.

Paul Sasik
I looked at this documentation. Looks pretty complicated, I must admit. A lot of details... Could you suggest a simple method to resize JPG files to a specified size (width and height in pixels) such that the output picture will be with best quality ? Thanks !
+2  A: 

You're not telling us the original format of your picture but you're saving as a JPEG:

b.Save(pathToOutputPicture, ImageFormat.Jpeg);

JPEG is a lossy compression format.

In addition to being lossy, JPEG also can output different quality (which is configurable).

This is what is happening to your file size: it is shrinking because you went, say, from a lossless format to the lossy JPEG or because you went from JPEG to JPEG-with-a-lower-quality.

Hence the size reduction.

Webinator
Actually, I went from JPG to JPG, but I was not aware of the quality that I used in the Save function. What is the default ? And I still don't understand whether the output picture in my example is worse than the original picture. I guess, yes, because the file size become pretty little, but I don't understand why it is worse. Thank you for your time !