---
title: "Resizing Image in C#"  
description: "In this article, we learn how to resize an image in c#. To complete this task firstly we design a user interface for resize image which is a combinati"  
author: "Anonymous User"  
published: 2011-08-06  
updated: 2019-12-10  
canonical: https://www.mindstick.com/articles/595/resizing-image-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 4 minutes  

---

# Resizing Image in C#

Here In this article, we learn how to resize an image in c#. To complete this task firstly we design a user interface for resizing image which is a [combination of some](https://www.mindstick.com/Articles/1144/displaying-an-array-of-images-in-picturebox-c-sharp) buttons, panels, TextBoxes, labels, picture boxes, and numeric dropdowns. After designing your user interface for resize image demo it looks like following\

![Resizing Image in C#](https://www.mindstick.com/mindstickarticle/b1984687-85b1-469f-b874-c6b0fd4f1d48/images/b3dc9175-1061-4566-a73f-969755ce6a14.png)

**1)** Now firstly creates some properties in your form class to store some information.

```
public Size OriginalImageSize {
get; set; } //Store original image size.public Size NewImageSize {
get; set; }  //Store new image size. 
```

**2)** Now click on the Browse Image button and write down the following code to browse the image in the picture box.

```
private void btnBrowseImage_Click(object sender,  EventArgs e){            OpenFileDialog openImage = new OpenFileDialog();            DialogResult result = openImage.ShowDialog();            if (result ==  DialogResult.OK)            {                //Load image in picture box and set size mode property of image as normal.                picImageResize.Load(openImage.FileName);                picImageResize.SizeMode = PictureBoxSizeMode.Normal;                  //Store source location of image in textbox.                txtImageOrignalLocation.Text = openImage.FileName;                  //Retrive height and width of image and store in OriginalImageSize variable.                int imgWidth = picImageResize.Image.Width;                int imgHeight = picImageResize.Image.Height;                OriginalImageSize = new Size(imgWidth, imgHeight);                  string imgSize = "Width " + imgWidth + " px  Height " + imgHeight+" px";                lblOriginalSizeValue.Text = imgSize;            }}
```

This code will store a description of the image in some controls and load images in the picture box.

**3)** Create a method namedScaleByPercent(Image imgPhoto, int percent) which will resize an image and return Image object which is resized by a percent.

```
//This method will resize image by scale percentace.        static Image ScaleByPercent(Image imgPhoto, int Percent)        {            float nPercent = ((float)Percent / 100);              int sourceWidth = imgPhoto.Width;      //store original width of source image.            int sourceHeight = imgPhoto.Height;    //store original height of source image.            int sourceX = 0;        //x-axis of source image.            int sourceY = 0;         //y-axis of source image.              int destX = 0;          //x-axis of destination image.            int destY = 0;          //y-axis of destination image.            //Calcuate height and width of resized image.            int destWidth = (int)(sourceWidth * nPercent);            int destHeight = (int)(sourceHeight * nPercent);              //Create a new bitmap object.            Bitmap bmPhoto = new Bitmap(destWidth, destHeight,                                     PixelFormat.Format24bppRgb);            //Set resolution of bitmap.            bmPhoto.SetResolution(imgPhoto.HorizontalResolution,                                    imgPhoto.VerticalResolution);            //Create a graphics object and set quality of graphics.            Graphics grPhoto = Graphics.FromImage(bmPhoto);            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;              //Draw image by using DrawImage() method of graphics class.            grPhoto.DrawImage(imgPhoto,                new Rectangle(destX, destY, destWidth, destHeight),                new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),                GraphicsUnit.Pixel);              grPhoto.Dispose();   //Dispose graphics object.            return bmPhoto;        }
```

**4)** On click event of OK button right down following code. This code will load resized images in the picture box and you can see the output.

```
private void btnOK_Click(object sender,  EventArgs e)        {            //When we click on ok button then call scalebypercent method which return resize image and store it Image type variable.            Image scaledImage = ScaleByPercent(picImageResize.Image, (int)numericUpDown1.Value);            picImageResize.Image = scaledImage;        //Display that image in picture box.              //Retrive width and height of image.            string imgSize = "Width " + scaledImage.Width + " px   Height " + scaledImage.Height + " px";            lblNewSizeValue.Text = imgSize;        }5) Now our final steps to save our resized image in a file. To Save resized images write down the following code. 
```

```
//When we click on save button then this image is saved in new location.        private void btnSaveImage_Click(object sender, EventArgs e)        {            SaveFileDialog sfd = new SaveFileDialog();    //Create an object of SaveFileDialog class.            sfd.DefaultExt = "jpeg";     //Set default extension of savefiledialog.            if (DialogResult.OK == sfd.ShowDialog())            {                string fileName = sfd.FileName;     //Store file name in string variable.                txtDestinationLocation.Text = fileName;   //Display saved file location in textbox.                Bitmap imgImage = new Bitmap(picImageResize.Image);     //Create an object of Bitmap class/                imgImage.Save(fileName, ImageFormat.Jpeg);    //Call save method of Bitmap class.            }        }
```

##### The output of the following code snippet is as follows

![Resizing Image in C#](https://www.mindstick.com/mindstickarticle/b1984687-85b1-469f-b874-c6b0fd4f1d48/images/ecb09af4-7f80-43b1-ae9c-14c1a442145f.png)

Click on the browse image button and load the image in the picture box.

![Resizing Image in C#](https://www.mindstick.com/mindstickarticle/b1984687-85b1-469f-b874-c6b0fd4f1d48/images/0eb0376a-e516-42f0-9a14-826ed5819824.png)

Enter the percentage value in numeric up down and click on the OK button.

![Resizing Image in C#](https://www.mindstick.com/mindstickarticle/b1984687-85b1-469f-b874-c6b0fd4f1d48/images/4efea35b-695a-4501-8eb6-2bbb4fd10bb6.png)

Now finally click on the save button and save resized image.

---

Original Source: https://www.mindstick.com/articles/595/resizing-image-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
