1

我正在尝试将图像大小调整为 500x500 像素。使用以下代码:

public static Bitmap ResizeImage(System.Drawing.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;
}

但这只是水平调整大小而不是裁剪!有人建议我将图像裁剪为居中的 1:1 比例,然后将其缩小。如果小于它,我还希望将图像大小增加到 500x500。

我怎样才能实现它?

4

1 回答 1

1

代替

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

利用

Rectangle cropRect = new Rectangle(0,0,500,500);
graphics.DrawImage(image, destRect, cropRect, GraphicsUnit.Pixel);

您还可以阅读更多关于DrawImageUnscaledAndClipped 裁剪目的的信息。

于 2015-09-03T09:38:30.427 回答