How to Create Thumbnail Images in C#

來源:互聯網
上載者:User

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 :

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.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.