ARTICLE

Create Image Thumbnails in VB.NET

Posted by Mahesh Chand Articles | GDI+ in VB.NET June 11, 2010
The code snippet and project attached in this article demonstrates how to create a control that displays thumbnails of images using GDI+ and VB.NET.
 
Reader Level:

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 Sub BrowseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BrowseButton.Click

        Dim OpenFileDialog1 As New OpenFileDialog

        OpenFileDialog1.InitialDirectory = "C:\"

        OpenFileDialog1.RestoreDirectory = True

 

        OpenFileDialog1.Title = "Browse Text Files"

        OpenFileDialog1.DefaultExt = "txt"

        OpenFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"

        OpenFileDialog1.FilterIndex = 2

        OpenFileDialog1.CheckFileExists = True

        OpenFileDialog1.CheckPathExists = True

 

        OpenFileDialog1.ReadOnlyChecked = True

        OpenFileDialog1.ShowReadOnly = True

 

        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

            TextBox1.Text = OpenFileDialog1.FileName

        End If

    End Sub

The code for Browse Multiple Files button click event handler looks like following.

    Private Sub BrowseMultipleFilesButton_Click(ByVal sender As System.Object, _

              ByVal e As System.EventArgs) Handles BrowseMultipleFilesButton.Click

        Dim OpenFileDialog1 As New OpenFileDialog

        OpenFileDialog1.Filter =

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

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

 

        OpenFileDialog1.Multiselect = True

        OpenFileDialog1.Title = "Select Photos"

 

        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

            For Each file As String In OpenFileDialog1.FileNames

                Dim imageControl As New PictureBox()

                imageControl.Height = 100

                imageControl.Width = 100

                Dim myCallback As New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)

                Dim myBitmap As New Bitmap(file)

                Dim myThumbnail As Image = myBitmap.GetThumbnailImage(96, 96, myCallback, IntPtr.Zero)

                imageControl.Image = myThumbnail

 

                PhotoGallary.Controls.Add(imageControl)

            Next

        End If

    End Sub

 

    Public Function ThumbnailCallback() As Boolean

        Return False

    End Function




Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor