Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake thumbnails and a web page to display the images in a directory in Visual Basic 6
DescriptionThis example shows how to make thumbnails and a web page to display the images in a directory in Visual Basic 6.
Keywordsfiles, internet, web, thumbnails, thumbnail images, web pages, HTML, Visual Basic 6, VB 6
CategoriesFiles and Directories, Internet, Utilities
 

This example searches a directory and builds a thumbnail image for each of the image files it finds there. It also builds a web page that displays the thumbnails and links to the full-scale images. It puts all of the files (original images, thumbnails, and web page) in the directory of your choice.

The following code shows the MakeWebPage method that does most of the work.

 
' Make the web page and thumbnails.
Private Sub MakeWebPage(ByVal input_dir As String, ByVal _
    output_dir As String, ByVal url_prefix As String, ByVal _
    thumb_width As Integer, ByVal thumb_height As Integer, _
    ByVal webpage_name As String)
Dim filenum As Integer
Dim html_filename As String
Dim files As Collection
Dim image_filename As Variant
Dim src_filename As String
Dim dest_filename As String
Dim original_width As Integer
Dim original_height As Integer
Dim shrunk_width As Integer
Dim shrunk_height As Integer
Dim scale1 As Double
Dim scale2 As Double
Dim thumb_scale As Double
Dim pos As Integer
Dim thumb_filename As String

    ' Open the HTML file.
    html_filename = output_dir & webpage_name
    filenum = FreeFile
    Open html_filename For Output As #filenum
    
    ' Make a list of the image files.
    Set files = FindFiles(input_dir, "*.jpg;*.bmp;*.gif")

    ' Process the files.
    For Each image_filename In files
        ' Copy the file to the destination directory.
        src_filename = input_dir & image_filename
        dest_filename = output_dir & image_filename
        FileCopy src_filename, dest_filename

        ' Get the image.
        picImage.Picture = LoadPicture(src_filename)

        ' Get the original size.
        original_width = picImage.ScaleWidth
        original_height = picImage.ScaleHeight

        ' Shrink the image.
        scale1 = thumb_width / original_width
        scale2 = thumb_height / original_height
        If (scale1 < scale2) Then
            thumb_scale = scale1
        Else
            thumb_scale = scale2
        End If
        shrunk_width = CInt(original_width * thumb_scale)
        shrunk_height = CInt(original_height * thumb_scale)
        picThumbnail.Width = Me.ScaleX(shrunk_width, _
            vbPixels, ScaleMode)
        picThumbnail.Height = Me.ScaleY(shrunk_height, _
            vbPixels, ScaleMode)

        ' Copy the image at reduced scale.
        picThumbnail.PaintPicture _
            picImage.Picture, _
            0, 0, shrunk_width, shrunk_height, _
            0, 0, original_width, original_height

        ' Save the thumbnail image.
        pos = InStrRev(image_filename, ".")
        thumb_filename = Left$(image_filename, pos - 1) & _
            "_thumb.bmp"
        SavePicture picThumbnail.Image, output_dir & _
            thumb_filename

        ' Add the thumbnail image to the HTML page.
        Print #filenum, _
            "<a href=""" & url_prefix & image_filename & _
                """>" & _
            "<img src=""" & url_prefix & thumb_filename & _
                """>" & _
            "</a>"
    Next image_filename

    ' Close the HTML file.
    Close #filenum

    MsgBox "Processed " & files.Count & " images."
End Sub
 
The method starts by opening a file to hold the new web page. Next it calls the FindFiles function to get files in the target directory that have jpg, bmp, or gif extensions. See the example Search for files that match multiple patterns in Visual Basic 6 for a description of the FindFiles method.

For each file it found, the code copies the file to the output directory. It then loads the file and calculates how big it should make the thumbnail to fit in the desired size without distorting it. The code makes a thumbnail image of that size and copies the original image into it. The method saves the thumbnail and finally writes a line into the web page to display the thumbnail, linking it to the full-size image.

 
 
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated