Category : C# | Author : Chtiwi Malek | First posted : 6/2/2012 | Updated : 12/6/2012
Tags : c#, .net, winforms, resize, picture, thumbnail, miniature, preview, image, photo
Resize Images and keep aspect ratio & proportions in C#

Resize Images and keep aspect ratio & proportions in C#

In this post I’m sharing this small class I wrote to resize pictures, 3 possible modes are:

1- Set the destination Width and Height (but this can result in skewed images when the aspect ratio is not identical)
  >> Call the function and set a fixed FinalWidth and a fixed FinalHeight

2- Maintain aspect ratio, and specify the desired image Width.
  >> Call the function with fixed FinalWidth and set FinalHeight to 0

3- Maintain aspect ratio, and specify the desired image Height.
  >> Call the function with fixed FinalHeight and set FinalWidth to 0

the Org parameter is the full original file path on the disk, and Des is the destination file path, also with ImageQuality you can specify the quality of the generated image (Jpg), lower quality means smaller file size.
public static void Resize_Picture(string Org, string Des, int FinalWidth, int FinalHeight, int ImageQuality)
{
    System.Drawing.Bitmap NewBMP;
    System.Drawing.Graphics graphicTemp;
    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Org);
	
    int iWidth;
    int iHeight;
    if ((FinalHeight == 0) && (FinalWidth != 0))
    {
        iWidth = FinalWidth;
        iHeight = (bmp.Size.Height * iWidth / bmp.Size.Width);
    }
    else if ((FinalHeight != 0) && (FinalWidth == 0))
    {
        iHeight = FinalHeight;
        iWidth = (bmp.Size.Width * iHeight / bmp.Size.Height);
    }
    else
    {
        iWidth = FinalWidth;
        iHeight = FinalHeight;
    }
	
    NewBMP = new System.Drawing.Bitmap(iWidth, iHeight);
    graphicTemp = System.Drawing.Graphics.FromImage(NewBMP);
    graphicTemp.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
    graphicTemp.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
    graphicTemp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    graphicTemp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    graphicTemp.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
    graphicTemp.DrawImage(bmp, 0, 0, iWidth, iHeight);
    graphicTemp.Dispose();
    System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
    System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, ImageQuality);
    encoderParams.Param[0] = encoderParam;
    System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
    for (int fwd = 0; fwd <= arrayICI.Length - 1; fwd++)
    {
        if (arrayICI[fwd].FormatDescription.Equals("JPEG"))
        {
            NewBMP.Save(Des, arrayICI[fwd], encoderParams);
        }
    }

    NewBMP.Dispose();
    bmp.Dispose();
}
Have fun and don't hesitate to comment or ask questions.
About the author :
Malek Chtiwi is the man behind Codicode.com
34 years old full stack developer.
Loves technology; but also likes design, photography and composing music.
Comments & Opinions :
Very good
very good, worked perfectly..
thanks for sharing.
- by Joe on 6/2/2012
Getting error (A generic error occurred in GDI+.)
Hi Sir,
i am also getting a gdi+ error in this code ..
error name (A generic error occurred in GDI+.)
can u help me in this sceneario

thanks in advance
- by humair on 4/1/2016
Fix size image cropping using Thumbnail in asp.net C#
Hii Sir,
i want to use  Fix size crop image using Thumbnail  with correct aspect ratio ....
How to d it ????

 
- by farhat on 11/29/2016
Save the image to database
Hello,

With the same approach is it possible to save the image to Datbase. I'm using mvc and sql server. Right now your code save the resized image to file system. I would like to know is it possible to save it to the sql server in varbinary(max) format ?

Thanks,
Sandhya
- by sandhya on 4/11/2017
Image is rotated
Hi,

​Thanks for sharing this code. For some reason two out of 33 images I am resizing are rotatred 90 degrees. Any ideas.

Thanks
Tom
- by Tom on 11/12/2017
You should try call function
//You should try call function
Resize_Picture("d:\\img.jpg","d:\\des.jpg",400,0,300);
- by Doan Phuong on 9/28/2018
Leave a Comment:
Name :
Email : * will not be shown
Title :
Comment :