0

I have the following:

using (bmp == new Bitmap(50, 50)) {
    using (g == Graphics.FromImage(bmp)) {
        g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic;
        g.DrawImage(img, 0, 0, 50, 50);
    }
}

The user provides my app an image which my app has to resize and also fit the entire image.

The problem with the above code is that it stretches the image, not fit (as a 4:3 movie fits in a widescreen TV leaving blackbars).

Does anyone have any solution for this? Also, I would prefer not to use GDI.

2

1 Answer 1

1

You need to take the size of the input image into account. Here is a code snippet that should get you into the right direction:

int x1 = 0, y1 = 0, x2 = 50, y2 = 50;
if (img. Width <= img.Height) 
{
   // compute x1, y1 to fit img horizontally into bmp
}
else
{
   // compute y1, y2 to fit img vertically into bmp
}

g.DrawImage(img, x1,y1, x2,y2);

Also notice that you are asking for resizing an image in WPF but using System.Drawing which uses GDI+ under the hood.

Not the answer you're looking for? Browse other questions tagged or ask your own question.