How to resize a image file using C#

In this article I have shown that how to resize any image file by using only a function 'ResizeImageFile' in C# programming language. Only you need to put the source image file, your desired width & height of image file as parameter of this function, then it will return your expected resized image file.

In our daily life, We need to work with image file for many purposes. To resize the image is one important task out of all image processing functionalities. I have tried to give a very simple function 'ResizeImageFile' for this purpose. There are three parameters in this function, the source image which image file you want to resize, your desired width & height. The function return a your expected resized image file. The neccessary source code is given below:

Inside the function, very simple mathematical calculation has been used to deduce the width & height of desired image file to keep constant the aspect ratio of original/source image file. New canvas for output image has been created by using new desired width & height. Now source image is drawn on the canvas with Graphics function.

 public Image ResizeImageFile(Bitmap InputImg, int OutputWidth, int OutputHeight)
int OriginalWidth = InputImg.Width;
int OriginalHeight = InputImg.Height;
float ratioX = (float)Convert.ToInt16(OutputWidth) / (float)OriginalWidth;
float ratioY = (float)Convert.ToInt16(OutputHeight) / (float)OriginalHeight;
float ratio = Math.Min(ratioX, ratioY);
int NewWidth = (int)(OriginalWidth * ratio);
int NewHeight = (int)(OriginalHeight * ratio);
Image OutputImage = new Bitmap(NewWidth, NewHeight);
using (Graphics GFX = Graphics.FromImage(OutputImage))
GFX.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
GFX.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
GFX.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
GFX.DrawImage(InputImg, new Rectangle(0, 0, NewWidth, NewHeight));
return OutputImage;


Author: Phagu Mahato28 Aug 2013 Member Level: Gold   Points : 8

Thanks for posting your valuable code snippet for resize a image . With the help of below code you can cropping your image.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestResizeCrop
public delegate void CroppingEventHandler(object sender, CroppingEventArgs e);

public partial class frmCropping : Form
private int cropingX;
private int cropingY;
private int cropingHeight;
private int cropingWidth;

public event CroppingEventHandler cropOK;

public frmCropping()

private void butCrop_Click(object sender, EventArgs e)
if (this.tableX.Text != "" && this.tableY.Text != "" &&
this.tableHeight.Text != "" && this.tableWidth.Text != "")
this.cropingX = Convert.ToInt32(this.tableX.Text);
this.cropingY = Convert.ToInt32(this.tableY.Text);
this.cropingHeight = Convert.ToInt32(this.tableHeight.Text);
this.cropWidth = Convert.ToInt32(this.tableWidth.Text);


CroppingEventArgs cropEventArgs = new CroppingEventArgs(this.cropX, this.cropingY, this.cropingHeight, this.cropingWidth);

protected virtual void CroppingOK(CroppingEventArgs eventArgs)
if (this.cropOK != null)
this.cropOK(this, eventArgs);

public class CroppingEventArgs : EventArgs
public int X;
public int Y;
public int Height;
public int Width;

public CroppingEventArgs(int x, int y, int height, int width)
this.X = x;
this.Y = y;
this.Height = height;
this.Width = width;

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: