How to Create Thumbnail Images in C#

时间:2022-03-12 00:26:08

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.

C#

private void btnOpen_Click(object sender, EventArgs e)

        {

if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)

            {

                txtFileNm.Text = openFileDialog1.FileName;

            }

        }

 

Step 4: Next, on the ‘btnGenerateThumbnail’ click, add the following code:

C#

// Declare a class level variable

Image imgThumb = null;

private void btnGenerateThumbnail_Click(object sender, EventArgs e)

        {

try

            {

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());

this.Refresh();

                }

            }

catch

            {

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.

C#

private void Form1_Paint(object sender, PaintEventArgs e)

        {

if(imgThumb != null)

            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 :

How to Create Thumbnail Images in C#

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.