Link to home
Create AccountLog in
Avatar of sggexchange
sggexchangeFlag for United States of America

asked on

ASP.Net C# - Image Resize increases file size (I need to reduce the image file size after resizing)

I have an image resize script that resizes my images with no problem. However, after it's resized the file size of the image ends up being bigger than the original. Being the:
- original image (.jpg) --> Dimensions: 827 x 500 and Size: 283 KB
- after resize (.jpg) --> Dimensions: 583 x 352 and Size: 579 KB

Is there a way to avoid this increment on size or maybe a compression or optimization script that I can use afterwards that will reduce the file size

Here is my resizing code:
public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
        {
            System.Drawing.Bitmap bmpOut = null;
            try
            {
                Bitmap loBMP = new Bitmap(lcFilename);
                ImageFormat loFormat = loBMP.RawFormat;
 
                decimal lnRatio;
                int lnNewWidth = 0;
                int lnNewHeight = 0;
 
                // If the image is smaller than a thumbnail just return it
                //if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
                //return loBMP;
 
                if (lnWidth < lnHeight)
                {
                    lnRatio = (decimal)lnWidth / loBMP.Width;
                    lnNewWidth = lnWidth;
                    decimal lnTemp = loBMP.Height * lnRatio;
                    lnNewHeight = (int)lnTemp;
                }
                else
                {
                    lnRatio = (decimal)lnHeight / loBMP.Height;
                    lnNewHeight = lnHeight;
                    decimal lnTemp = loBMP.Width * lnRatio;
                    lnNewWidth = (int)lnTemp;
                }
 
                bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
                Graphics g = Graphics.FromImage(bmpOut);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
                g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
 
                loBMP.Dispose();
            }
            catch
            {
                return null;
            }
 
            return bmpOut;
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Göran Andersson
Göran Andersson
Flag of Sweden image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
The constructor for Bitmap defaults to PixelFormat.Format32bppArgb, when not specified:

public Bitmap(int width, int height) : this(width, height, PixelFormat.Format32bppArgb)
{
}

Bob

 
> The constructor for Bitmap defaults to PixelFormat.Format32bppArgb, when not specified

Yes, but that is irrelevant. JPEG images are always saved as 24bpp, regardless of the PixelFormat of the Bitmap object.
Avatar of sggexchange

ASKER

That worked great!

Thanks GreenGhost
What about the rest of file types? Is there a generic method that works with all image file types?