ARTICLE

Create Image Thumbnails in C#

Posted by Mahesh Chand Articles | GDI+ & Graphics June 10, 2010
The code snippet and project attached in this article demonstrates how to create a control that displays thumbnails of images using GDI+ and C#.
Reader Level:
Download Files:
 

The code for Browse Multiple Files button click event handler looks like following that let users select multiple images and show their thumbnails. Download the attached project for more details. The application looks like this:

thumbnailImg.jpg

It is is a Windows Forms application developed using Visual Studio 2010.

private void BrowseMultipleButton_Click(object sender, EventArgs e)

{

        this.openFileDialog1.Filter =

        "Images (*.BMP;*.JPG;*.GIF,*.PNG,*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" +

        "All files (*.*)|*.*";

 

    this.openFileDialog1.Multiselect = true;

    this.openFileDialog1.Title = "Select Photos";

 

        DialogResult dr = this.openFileDialog1.ShowDialog();

        if (dr == System.Windows.Forms.DialogResult.OK)

        {

            foreach (String file in openFileDialog1.FileNames)

            {

                try

                {

                    PictureBox imageControl = new PictureBox();

                    imageControl.Height = 100;

                    imageControl.Width = 100;

 

                    Image.GetThumbnailImageAbort myCallback =

                            new Image.GetThumbnailImageAbort(ThumbnailCallback);

                    Bitmap myBitmap = new Bitmap(file);

                    Image myThumbnail = myBitmap.GetThumbnailImage(96, 96,

                        myCallback, IntPtr.Zero);

                    imageControl.Image = myThumbnail;

 

                    PhotoGallary.Controls.Add(imageControl);

                }

                catch (Exception ex)

                {

                    MessageBox.Show("Error: " + ex.Message);

                }

            }

        }

}

 

public bool ThumbnailCallback()

{

    return false;

}

Login to add your contents and source code to this article
comments
COMMENT USING
PREMIUM SPONSORS
Nevron Software is a global leader in component based data visualization technology for a diverse range of Microsoft centric platforms. Nevron Data Visualization components are used by many companies, educational and government organizations around the world.
Nevron Chart
SPONSORED BY
Nevron Diagram