Create Image Thumbnails in VB.NET

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.
  • 14625
 

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

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.