0

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;
    }
Community
  • 1
  • 1
C Smith
  • 778
  • 2
  • 14
  • 31
  • I think posX and posY should be both 0 - you are starting fresh, so you want to fill the whole area. And thumbnail should be created with the new size and not the original (not canvasW and canvasH). – keenthinker Nov 22 '13 at 11:16

2 Answers2

6

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.

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
  • Of course! Graphics is saving into Thumbnail, not image. Man, I'm either stupid or tired. Can't believe I missed that =D Thank you very much for your help! – C Smith Nov 22 '13 at 11:32
0

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

BalaRam
  • 59
  • 7