Click here to Skip to main content
15,888,173 members
Please Sign up or sign in to vote.
3.50/5 (3 votes)
See more:
This is the code I am using to resize an image (the actual kb/mb size)

C#
public static System.Drawing.Image ScaleImage(System.Drawing.Image image, int maxHeight)
    {
        var ratio = (double)maxHeight / image.Height;
        var newWidth = (int)(image.Width * ratio);
        var newHeight = (int)(image.Height * ratio);
        var newImage = new Bitmap(newWidth, newHeight);

        using (var g = Graphics.FromImage(newImage))
        {
            g.DrawImage(image, 0, 0, newWidth, newHeight);
        }
        return newImage;
    }


But when I use this code, it actually makes the image size(kb size) bigger than what it was. Although the physical size (width x height) is smaller.
Where am I going wrong?
Posted
Comments
Mitchell J. 13-Dec-13 6:16am    
What example sizes are in play here? What file types are causing the issue?
Member 10395722 17-Dec-13 7:21am    
For example a file of 90Kb after resize is 171Kb and the file type I am using is JPEG
BillWoodruff 13-Dec-13 6:46am    
You are creating a new image with a format of 32 bits-per-pixel ARGB: your source image probably has less bit-depth. So, file size is larger.

The first thing to do is look at your numbers: put a breakpoint on the line:
C#
var newImage = new Bitmap(newWidth, newHeight);
and use the debugger to examine newHeight, newWidth, and ratio
I suspect that they will not be what you expect.

Have you considered what happens in your current image has a lower height than maxHeight?


BTW: How do you know the kb / mb size is bigger? Are you saving the new image and comparing file sizes? If so, are they the same type of file, and an uncompressed format?
 
Share this answer
 
Comments
Member 10395722 17-Dec-13 7:15am    
I was saving the files in folders and then comparing the file sizes. They are the same file type and very sure they are uncompressed since I don't compress it anywhere.
OriginalGriff 17-Dec-13 7:23am    
Doesn't matter if you don't compress them: some file types always contain compressed data.
JPG for example uses a "lossy" compression technique to reduce file size - depending on what application a JPG file is written with the amount of compression can be very different.
When you resize an image (even if you reduce the size) you can end up with a bigger file because the data being compressed is different!

Similarly, even if your file type is BMP which isn;t compressed, if the original image is 8 bits per pixel and the new images is 32 bits (normally for .NET) then the new, smaller resolution file can easily be bigger than the original!
You should check that the PixelFormat of the original image and of the resized one are the same (see, for instance: Bitmap Constructor (Int32, Int32, PixelFormat)[^]).
 
Share this answer
 
I decided to use the current code that I have. What I observed is that bigger images for example 2MB gets resized to a smaller KB Size and smaller images of 80KB get bigger.

What BillWoodruff said in a comment is probably what is happening here. I don't know if there is a fix for this but for now i'll use it as is because it makes very big files atleast a little bit smaller
 
Share this answer
 
try saving in png extension because that compacts some image automatically.
 
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