Click here to Skip to main content
15,898,991 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I write a program that resize 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 ? In what ?

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 !

Hi,
Thank you very much for your answers !
I don't really want to "resize" to the original size.
That was just an example.
Actually, I write a program that resizes pictures to the desired size (width and height in pixels).
I've noticed that even if the picture size decreases a little, the file size decreases very much. I just want to understand why.
Then I tried to "resize" to the original size, and saw that the file size become pretty little. This is really strange to me !
I still don't understand whether the output picture in this case is worse than the original picture.
In my program I'm interested to resize JPEG pictures in order to send them by email. I know that there are many programs that do that, but I want to write my own program.
Could you suggest how to resize JPEGs such that the output picture will be with highest quality ?
Thanks !
Posted
Updated 23-Jan-10 10:35am
v2

In the first place, why should you "resize" an image to the same size?

Anyway... I understand that the image you have is a jpeg. Opening and resaving a jpeg is almost always losing quality.

You should really use png instead.

Secondly, ppi, dpi or whatever is not important. This is not about quality and it affects neither file size nor screen view quality. It only affects printing size.

If you want to, you can try:
b.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
 
Share this answer
 
I deleted the 'answer' you posted because it was really just restating the question. Edit your post for that.


moroshko wrote:
I've noticed that even if the picture size decreases a little, the file size decreases very much. I just want to understand why.


You were told why. JPEG is lossy, and so your original JPEG is already not a perfect copy of hte original bitmap. Applying the default level of JPEG compression, loses even more. You should save in a lossless format if you want to keep all your data. However, as someone already told you, you can add some code to save using a compression of 100% ( which is still lossy, but will make the file bigger for you ).


moroshko wrote:
Could you suggest how to resize JPEGs such that the output picture will be with highest quality ?


There is nothing anyone can add to the answers you were already given.
 
Share this answer
 
And, if you really have to keep it jpeg, take a look at http://en.wikipedia.org/wiki/JPEG#Lossless_editing

So, you can rotate, crop (and similar), but sadly, it does not say anything about resizing.
 
Share this answer
 
v3
By the way, if you only need a thumbnail, use GetThumbnailImage.
See an example here.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900