将JPEG图像调整为固定宽度,同时保持纵横比不变

cll*_*pse 5 c# aspect-ratio image-resizing

如何在保持纵横比的同时将JPEG图像调整为固定宽度?以简单的方式,同时保持质量.

rbo*_*man 19

这将仅在垂直轴上缩放:

    public static Image ResizeImageFixedWidth(Image imgToResize, int width)
    {
        int sourceWidth = imgToResize.Width;
        int sourceHeight = imgToResize.Height;

        float nPercent = ((float)width / (float)sourceWidth);

        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);

        Bitmap b = new Bitmap(destWidth, destHeight);
        Graphics g = Graphics.FromImage((Image)b);
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;

        g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
        g.Dispose();

        return (Image)b;
    }
Run Code Online (Sandbox Code Playgroud)


Ste*_*ens 1

如果要将宽度减少 25% 至固定值,则必须将高度减少 25%。

如果要将宽度增加 25% 达到固定值,则必须将高度增加 25%。

这真的很简单。