Question

I have a function which takes in an image and resizes it to fit a canvas while maintaining its aspect ratio. This code is only a minorly modified version of the code from this answer: c# Image resizing to different size while preserving aspect ratio

For this example, my canvas height is 642, my canvas width is 823.

When I run the below function, the line

graphic.DrawImage(image, posX, posY, newWidth, newHeight);

seemingly does nothing to the image size. Going in:

Image.Height == 800, 
Image.Width == 1280.
newHeight = 514,
newWidth == 823

AFTER running graphic.DrawImage

Image.Height == 800, 
Image.Width == 1280.

As you can see, Image's height and width are unchanged.

Does anyone see a gapingly obvious error that would cause this to happen? Thank you!

    private Bitmap resizeImage(Bitmap workingImage,
                         int canvasWidth, int canvasHeight)
    {
        Image image = (Bitmap)workingImage.Clone();

        System.Drawing.Image thumbnail =
            new Bitmap(canvasWidth, canvasHeight); 

        double ratioX = (double)canvasWidth / (double)workingImage.Width;
        double ratioY = (double)canvasHeight / (double)workingImage.Height;

        double ratio = ratioX < ratioY ? ratioX : ratioY;

        int newHeight = Convert.ToInt32((double)workingImage.Height * ratio);
        int newWidth = Convert.ToInt32((double)workingImage.Width * ratio);

        int posX = Convert.ToInt32((canvasWidth - ((double)workingImage.Width * ratio)) / 2);
        int posY = Convert.ToInt32((canvasHeight - ((double)workingImage.Height * ratio)) / 2);

        using (Graphics graphic = Graphics.FromImage(thumbnail))
        {
            graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
            graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

            graphic.Clear(SystemColors.Control); 
            graphic.DrawImage(image, posX, posY, newWidth, newHeight); //<--- HERE
        }

        System.Drawing.Imaging.ImageCodecInfo[] info =
                         System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
        System.Drawing.Imaging.EncoderParameters encoderParameters;
        encoderParameters = new System.Drawing.Imaging.EncoderParameters(1);
        encoderParameters.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality,
                         100L);

        return workingImage;
    }
Was it helpful?

Solution

Size of your image is defined here

Image image = (Bitmap)workingImage.Clone();

This

graphic.DrawImage(image, posX, posY, newWidth, newHeight);

only draws the image with specified arguments, but it does not mean that the image size gets changed. In other words, drawing an image simply does not change its size, it just takes the image and draws it on a canvas as you wish.

OTHER TIPS

Image Resizing functanality pupose see the below link

http://www.codeproject.com/Articles/30524/An-Easy-to-Use-Image-Resizing-and-Cropping-Control?msg=5203911#xx5203911xx

This link content may help you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top