Laziale
Laziale

Reputation: 8225

Resize image with percentage value in C#

I would like to know if its possible in C# to resize image by given percentage proportionally.

For example I have image from which I want to create thumbnail, and I want to resize to 25% of the size, is it possible to put the percentage and apply that within specific method?

Upvotes: 1

Views: 5222

Answers (1)

Jakob Olsen
Jakob Olsen

Reputation: 823

I use this method to resize images in an application i made. It uses 'System.Drawing.2D'

public static 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;
    }

Upvotes: 3

Related Questions