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

5

5

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

World answered 21/11, 2011 at 15:56 Comment(0)
E
20

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;
    }
Egotist answered 17/7, 2012 at 18:14 Comment(0)
L
1

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.

Ln answered 21/11, 2011 at 16:2 Comment(0)
E
1

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.

Earle answered 21/11, 2011 at 16:8 Comment(0)
S
0

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));
Sheltonshelty answered 21/11, 2011 at 16:3 Comment(0)
G
0

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

Goldina answered 21/11, 2011 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.