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 buttons, panels, TextBoxes, labels, picture boxes, and numeric dropdowns. After designing your user interface for resize image demo it looks like following
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
Click on the browse image button and load the image in the picture box.
Enter the percentage value in numeric up down and click on the OK button.
Now finally click on the save button and save resized image.
Anonymous User
24-Apr-2019Thanks for the guidance.
Uttam Misra
06-Aug-2011