How to Create Thumbnail Image in C#

By FoxLearn 7/18/2024 7:29:44 AM   3.97K
To create a thumbnail from an image in C#, you can use the System.Drawing namespace, which provides classes to work with images.

How To Generate Thumbnail Image In C#

Open your form designer, then drag a PictureBox, Button from the Visual Studio toolbox into your form desinger, then design a simple layout allows you to open an image, then get the thumbnail image as shown below.

c# create thumbnail image

Add a click event handler to the Convert button allows you to open an image, then convert to the thumbnail image.

// C# get thumbnail from image file
private void btnConvert_Click(object sender, EventArgs e)
{
    using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "JPEG|*.jpg" })
    {
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            Image image = Image.FromFile(ofd.FileName);
            pictureBox1.Image = image;
            pictureBox2.Image = image.GetThumbnailImage(70, 70, () => false, IntPtr.Zero);
        }
    }
}

How to convert image into thumbnail in C#?

You can also create a CreateThumbnailImage method allows you to create a thumbnail image from a larger image in c#.

// c# create thumbnail from image
public static Image CreateThumbnailImage(string url, int width = 65, int height = 80)
{
    Image image = Image.FromFile(url);
    Image thumb = image.GetThumbnailImage(width, height, () => false, IntPtr.Zero);
    image.Dispose();
    return thumb;
}

The GetThumbnailImage method is used to create the thumbnail. It resizes the image while preserving the aspect ratio.

Ensure that you have added a reference to the System.Drawing assembly in your project. You can do this by right-clicking on your project in Visual Studio, selecting "Add" > "Reference...", and then selecting System.Drawing from the ".NET" tab.

We often create a thumbnail image when uploading an image to a website. The thumbnail image will be displayed in the post's brief description.