terjetyl / Simple.ImageResizer

A simple c# image resizer with ScaleToFit and ScaleToFill using wpf libraries. Resizes to jpg, gif, png, tiff. Demosite is hosted at AppHarbor: http://imageresizer.apphb.com/
58 stars 27 forks source link

Images are being cropped instead for being resized #11

Open samir-syed opened 6 years ago

samir-syed commented 6 years ago

Below is the function I use to resize:

private static void Resize(string path, int width, int height, string destPath, bool crop = false)
{
    var resizer = new ImageResizer(path);
    resizer.Resize(width, height, crop, ImageEncoding.Jpg100);
    resizer.SaveToFile(destPath);
}

The image I am trying to resize is 2500 x 2500 in size (px) is as below: splashscreen

For example I am having issues resizing the above image to the following dimensions (in px):

1024 x 500 (Note the company name being cropped):

appstore-feature

720 x 1280 (Note the label being cropped):

screen-xhdpi-portrait

Am I doing something wrong here? Is there an option to ignore what happens to the resulting image and just resize the image. I mean an option to just ignore the aspect ratio and just resize it to the provided dimensions?

samir-syed commented 6 years ago

I found a snippet online which does what I need:

public static System.Drawing.Bitmap ResizeImage(Image image, int width, int height)
        {
            var destRect = new Rectangle(0, 0, width, height);
            var destImage = new Bitmap(width, height);

            destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

            using (var graphics = Graphics.FromImage(destImage))
            {
                graphics.CompositingMode = CompositingMode.SourceCopy;
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

                using (var wrapMode = new ImageAttributes())
                {
                    wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                    graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
                }
            }
            return destImage;
        }

And calling the above method from this:

private static void Resize(string path, int width, int height, string destPath)
        {
            //var resizer = new ImageResizer(path);

            //var byteArray = resizer.Resize(width, height, false, ImageEncoding.Jpg100);

            //resizer.SaveToFile(destPath);
            Image imgPhoto = Image.FromFile(path);
            Bitmap image = ResizeImage(imgPhoto, width, height);
            image.Save(destPath);
        }

I understand the images might end up being skewed but an optional parameter would help people like me to use this plugin.