How to zoom an image in C#

By FoxLearn 1/9/2025 8:49:48 AM   15.05K
To zoom an image in a PictureBox in C#, you can manipulate the Image property of the PictureBox by adjusting the SizeMode and scaling the image.

With C#, you can easily implement image zoom functionality using a PictureBox control and a few simple methods. This article demonstrates how to zoom an image dynamically based on user input, such as adjusting a TrackBar for scaling the image.

How to Zoom an Image Using PictureBox in C#?

Open Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "ImageZoom" and then click OK

Drag and drop the Label, TextBox, Button, PictureBox, and TrackBar control from the Visual Toolbox onto your form desinger, then design your form as shown below.

zoom image in c#

The user can load an image by clicking the btnOpen button. This opens a file dialog, allowing them to select a JPEG image, which is then displayed in the PictureBox.

private void btnOpen_Click(object sender, EventArgs e)
{
    //Open image
    using (OpenFileDialog ofd = new OpenFileDialog() { Multiselect = false, ValidateNames = true, Filter = "JPEG|*.jpg" })
    {
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            pictureBox1.Image = Image.FromFile(ofd.FileName);
            imgOriginal = pictureBox1.Image;
        }
    }
}

The zoom functionality is controlled by a TrackBar. As the user moves the slider, the trackBar1_Scroll event is triggered, and the image is resized according to the current value of the slider.

// zoom in picturebox c#
private void trackBar1_Scroll(object sender, EventArgs e)
{
    if (trackBar1.Value > 0)
    {
        // picturebox c# zoom image
        pictureBox1.Image = Zoom(imgOriginal, new Size(trackBar1.Value, trackBar1.Value));
    }
}

The Zoom method takes the original image and a Size object, which is based on the TrackBar value. The new size is calculated by adding a percentage of the image's width and height. The zoom is applied using the Graphics class to ensure the image scaling looks smooth and maintains its visual quality.

// c# zoom image file
Image Zoom(Image img, Size size)
{
    Bitmap bmp = new Bitmap(img, img.Width + (img.Width * size.Width / 100), img.Height + (img.Height * size.Height / 100));
    Graphics g = Graphics.FromImage(bmp);
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    return bmp;
}

The use of HighQualityBicubic interpolation mode ensures that the zoomed image maintains high visual quality, reducing the distortion often seen with simple scaling.

The Form1_FormClosing event ensures that the image resource is properly disposed of when the form is closed, preventing memory leaks.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (pictureBox1.Image != null)
        pictureBox1.Dispose();
}

Add code to button click event handler as below

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

namespace ImageZoom
{
    public partial class Form1 : Form
    {
        Image imgOriginal;

        public Form1()
        {
            InitializeComponent();
        }
    }
}

This simple yet powerful C# application demonstrates how to create a dynamic image zoom feature using a PictureBox and a TrackBar.

VIDEO TUTORIAL