在不保持纵横比的情况下调整图像大小

时间:2016-04-01 18:25:58

标签: c# .net bitmap gdi+ system.drawing

我需要将图像调整为特定大小而不考虑宽高比。此代码考虑了宽高比。如何忽略宽高比并进行大小调整。

xdotool windowminimize $( xdotool search --name "${TERMINAL_TITLE}" )

1 个答案:

答案 0 :(得分:3)

删除所有计算并使用

grPhoto.DrawImage(imgPhoto,
    new Rectangle(0, 0, newWidth, newHeight),
    new Rectangle(0, 0, sourceWidth, sourceHeight),
    GraphicsUnit.Pixel);

以下是整个方法:

public static Image Resize(Image source, int width, int height)
{
    if (source.Width == width && source.Height == height) return source;
    var result = new Bitmap(width, height, PixelFormat.Format24bppRgb);
    result.SetResolution(source.HorizontalResolution, source.VerticalResolution);
    using (var g = Graphics.FromImage(result))
        g.DrawImage(source, new Rectangle(0, 0, width, height), new Rectangle(0, 0, source.Width, source.Height), GraphicsUnit.Pixel);
    return result;
}