简体   繁体   中英

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

How would you resize a JPEG image, to a fixed width whilst keeping aspect ratio? In a simple way, whilst preserving quality.

This will scale in the vertical axis only:

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

If you are reducing the width by 25 percent to a fixed value, you must reduce the height by 25 percent.

If you are increasing the width by 25 percent to a fixed value, you must increasing the height by 25 percent.

It's really straight forward.

Assuming there is a ( double width ) variable:

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

In the end you´ll have a new image with widthxheight, then, you´ll need to flush the graphics e save the imgnew.

I think there are plenty of samples of this if you search for them. Here's the one I commonly use...

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

Example of the calling code...

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

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

A quick search on code project has found the following article. It allows for resizing of images which accepts a boolean to restrain the new image to keep the originals aspect ratio. I'm unsure of what the quality is like as no screenshots were provided. See the article here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM