簡體   English   中英

將JPEG圖像調整為固定寬度,同時保持寬高比不變

[英]Resize JPEG image to fixed width, while keeping aspect ratio as it is

如何在保持寬高比的同時將JPEG圖像調整為固定寬度? 以一種簡單的方式,同時保持質量。

這只會在垂直軸上縮放:

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

如果要將寬度減少25%至固定值,則必須將高度減少25%。

如果要將寬度增加25%至固定值,則必須將高度增加25%。

這真的很簡單。

假設有一個( 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);

最后,您將獲得一個新的寬度為xheight的圖像,然后需要刷新圖形並保存imgnew。

我認為如果您搜索它們,將會有很多示例。 這是我常用的那個...

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

快速搜索代碼項目已找到以下文章。 它允許調整圖像的大小,該圖像接受布爾值以限制新圖像以保持原始寬高比。 我不確定畫質如何,因為沒有提供截圖。 這里看到文章

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM