Making Image editing tool in C# – Resize an image

This is the next article of the ‘making Image editing tool’ project. In first article we have learnt how to show or open image file in vb.net application with very professional look.

Now in this article we will learn about resizing of the image.

Design Right panel with the help of tabcontrol, and DomainUpDown control, so that user can input percentage of original size of image.

Download Source Code

Put the values 1 to 1000 in domainupdown control

private void BindDomainIUpDown()
        {
            for (int i = 1; i <= 999; i++)
            {
                DomainUpDown1.Items.Add(i);
            }
            DomainUpDown1.Text = “100”;
        }

We declare two another variables modifiedSize and originalSize, OriginalSize refers to original width and height of image and modifiedSize refers to width and height of image after DomainUpDown control’s value selection.

     private Size OriginalImageSize;

      private Size ModifiedImageSize;

 We can calulate original size of the image like this

int imgWidth = Img.Width;

 int imghieght = Img.Height;

OriginalImageSize = new Size(imgWidth, imghieght);

 And modifiedSize:

private void DomainUpDown1_SelectedItemChanged(object sender, EventArgs e)
        {
            int percentage = 0;
            try
            {
                percentage = Convert.ToInt32(DomainUpDown1.Text);
                ModifiedImageSize = new Size((OriginalImageSize.Width * percentage) / 100, (OriginalImageSize.Height * percentage) / 100);
                SetResizeInfo();
            }
            catch (Exception ex)
            {
                MessageBox.Show(“Invalid Percentage”);
                return;
            }
         }

 

Resizing image:

 Now We have to resize image according to modifiedSize

private void btnOk_Click(object sender, EventArgs e)
        {
            Bitmap bm_source = new Bitmap(PictureBox1.Image);
            // Make a bitmap for the result.
            Bitmap bm_dest = new Bitmap(Convert.ToInt32(ModifiedImageSize.Width), Convert.ToInt32(ModifiedImageSize.Height));
            // Make a Graphics object for the result Bitmap.
            Graphics gr_dest = Graphics.FromImage(bm_dest);
            // Copy the source image into the destination bitmap.
            gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width + 1, bm_dest.Height + 1);
            // Display the result.
            PictureBox1.Image = bm_dest;
            PictureBox1.Width = bm_dest.Width;
            PictureBox1.Height = bm_dest.Height;
            PictureBoxLocation();
         }

 Picturebox will show image after resizing

So this is very easy to do this you can download sourcecode for better understanding.


Other related articles of this series:

Making image editing tool in C#

Making image editing tool in C#- Crop an image

Making image editing tool in C#- Brightness of an image

Making image editing tool in C#- Rotations of an image


2 thoughts on “Making Image editing tool in C# – Resize an image”

Comments are closed.