Home > DeveloperSection > Articles > Resizing Image in C#

Resizing Image in C#


C# C# 
Ratings:
1 Comment(s)
 24887  View(s)
Rate this:

Resizing Image in C#

In this article we learn that how to resize image in c#. To complete this task firstly we design a user interface for resize image which is combination of some buttons, panels, textboxes, labels, picture box and numeric drop down. After designing your user interface for resize image demo it looks like following

Resizing Image in C#

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 Browse Image button and write down following code to browse image in 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 description of image in some controls and load image in 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 image in picture box and you can see 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 file. To Save resized image write down 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.

            }

        }

Output of the following code snippet is as follows

Resizing Image in C#

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

Resizing Image in C#

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

Resizing Image in C#

Now finally click on save button and save resized image.


Resizing Image in C#

By Uttam Misra on   5 years ago
Nice Article..Well Explained..Keep writing.

Don't want to miss updates? Please click the below button!

Follow MindStick