How to Create Thumbnail Images in C# and VB.NET
A thumbnail is a small sized image. Creating a thumbnail using .NET is extremely simple. In this article, we will explore how to create a thumbnail image and display the thumbnail in our application. Follow these steps:
Step 1: Open Visual Studio 2005/2008. File > New > Project > Visual C# or Visual Basic > Windows Application. Enter the name of the application and click Ok.
Step 2: Drag and drop a label, 2 button controls and an OpenFileDialog component to the form. Rename them as follows:
Label1 – lblFile
Button1 – btnOpen
Button2 – btnGenerateThumbnail
TextBox – txtFileNm
OpenFileDialog – Set the Filter to ‘JPG Files|*.jpg’
Step 3: On the ‘btnOpen’ click, display the File Open dialog box and accept the selected .jpg file in the txtFileNm textbox.
private void btnOpen_Click(object sender, EventArgs e)
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
txtFileNm.Text = openFileDialog1.FileName;
Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As EventArgs)
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
txtFileNm.Text = openFileDialog1.FileName
Step 4: Next, on the ‘btnGenerateThumbnail’ click, add the following code:
// Declare a class level variable
Image imgThumb = null;
private void btnGenerateThumbnail_Click(object sender, EventArgs e)
Image image = null;
// Check if textbox has a value
if (txtFileNm.Text != String.Empty)
image = Image.FromFile(txtFileNm.Text);
// Check if image exists
if (image != null)
imgThumb = image.GetThumbnailImage(100, 100, null, new IntPtr());
MessageBox.Show("An error occured");
' Declare a class level variable
Dim imgThumb As Image = Nothing
Private Sub btnGenerateThumbnail_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim image As Image = Nothing
' Check if textbox has a value
If txtFileNm.Text <> String.Empty Then
image = Image.FromFile(txtFileNm.Text)
' Check if image exists
If Not image Is Nothing Then
imgThumb = image.GetThumbnailImage(100, 100, Nothing, New IntPtr())
MessageBox.Show("An error occured")
The code creates an Image object from the image supplied in the textbox. Using the Image.GetThumbnailImage(), the code then creates a thumbnail image with a size of 100*100.
The Image.GetThumbnailImage() takes in four arguments :
Width, in pixel of the thumbnail image that is to be generated
Height, in pixel of the thumbnail image that is to be generated
Callback, a Image.GetTumbnailImageAbort delegate to prematurely cancel execution
CallbackData, of type IntPtr to represent a pointer or a handle.
Step 5: The final step is to add the Paint event which is called using this.Refresh() in the button click. The thumbnail image is drawn on the form.
private void Form1_Paint(object sender, PaintEventArgs e)
if(imgThumb != null)
e.Graphics.DrawImage(imgThumb,30, 20, imgThumb.Width, imgThumb.Height);
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs)
If Not imgThumb Is Nothing Then
e.Graphics.DrawImage(imgThumb,30, 20, imgThumb.Width, imgThumb.Height)
Run the application, select the image and click on the Generate button. The preview will be similar to the image displayed below :
You can actually extend this example to 'save' the generated thumbnails. Something like a ‘Thumbnail Creator’ program. You can loop through a folder containing images and generate thumbnail images for all the images in the folder. I would encourage you to try out these ideas.
I hope this article was useful and I thank you for viewing it.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!