将JPEG图片的大小调整为固定宽度,同时保持其原始纵横比。

5

如何调整JPEG图像的大小,以固定宽度保持纵横比?简单易懂地保留质量。

5个回答

20

这将只在垂直轴上进行缩放:

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

1

如果您将宽度减少25%到一个固定值,那么必须将高度也减少25%。

如果您将宽度增加25%到一个固定值,那么必须将高度也增加25%。

这非常简单明了。


1
假设有一个(double width)变量:
Image imgOriginal = Bitmap.FromFile(path);
double height = (imgOriginal.Height * width) / imgOriginal.Width;
Image imgnew = new Bitmap((int)width, (int)height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(imgnew);
g.DrawImage(imgOriginal, new Point[]{new Point(0,0), new Point(width, 0), new Point(0, height)}, new Rectangle(0,0,imgOriginal.Width, imgOriginal.Height), GraphicsUnit.Pixel);

最终你会得到一个宽为width、高为height的新图片,然后需要刷新图形并保存imgnew。

0
在 Code Project 上进行快速搜索,找到了以下文章。它允许调整图像大小,并接受一个布尔值来限制新图像保持原始纵横比。由于没有提供屏幕截图,我不确定其质量如何。请查看此文章here

0

如果你搜索一下,我认为会有很多这方面的示例。这是我常用的一个...

    public static Stream ResizeGdi(Stream stream, System.Drawing.Size size)
    {
        Image image = Image.FromStream(stream);

        int width = image.Width;
        int height = image.Height;

        int sourceX = 0, sourceY = 0, destX = 0, destY = 0;

        float percent = 0, percentWidth = 0, percentHeight = 0;
        percentWidth = ((float)size.Width / (float)width);
        percentHeight = ((float)size.Height / (float)height);

        int destW = 0;
        int destH = 0;

        if (percentHeight < percentWidth)
        {
            percent = percentHeight;
        }
        else
        {
            percent = percentWidth;
        }

        destW = (int)(width * percent);
        destH = (int)(height * percent);

        MemoryStream mStream = new MemoryStream();

        if (destW == 0
            && destH == 0)
        {
            image.Save(mStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            return mStream;
        }

        using (Bitmap bitmap = new Bitmap(destW, destH, System.Drawing.Imaging.PixelFormat.Format48bppRgb))
        {
            using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap))
            {
                //graphics.Clear(Color.Red);
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphics.DrawImage(image,
                    new Rectangle(destX, destY, destW, destH),
                    new Rectangle(sourceX, sourceY, width, height),
                    GraphicsUnit.Pixel);
            }

            bitmap.Save(mStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        }

        mStream.Position = 0;
        return mStream as Stream;
    }

调用代码示例...

Stream stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.None);

resizedStream = ImageUtility.ResizeGdi(stream, new System.Drawing.Size(resizeWidth, resizeHeight));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接