c#用百分比值调整图像大小

时间:2015-11-13 11:00:42

标签: c# c#-4.0 image-resizing

我想知道它是否可能在C#中按比例调整给定百分比的图像。

例如,我有想要创建缩略图的图像,并且我想调整大小的25%,是否可以将百分比放在特定方法中?

由于

1 个答案:

答案 0 :(得分:3)

我使用此方法在我制作的应用程序中调整图像大小。它使用' 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;
    }