Create Image Thumbnails in C#


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;

}


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.