Welcome Guest!
 VB 6 Helper
 Previous Message All Messages Next Message 
VB 6 Helper Newsletter  Rod Stephens
 Jan 21, 2011 13:36 PST 

Just a short one this time.

Have a great week and thanks for subscribing!


Twitter feeds:

    VB6 Contents:
1. New HowTo: Search for files that match multiple patterns in Visual
Basic 6
2. New HowTo: Make thumbnails and a web page to display the images in a
directory in Visual Basic 6
1. New HowTo: Search for files that match multiple patterns in Visual
Basic 6

The Dir function lets you search a directory for files. The FindFiles
method shown in the following code searches for files that match
multiple patterns.

' Return a list of files that match the patterns.
Private Function FindFiles(ByVal dirname As String, ByVal patterns As
String) As Collection
Dim pattern_array() As String
Dim pattern As Variant
Dim files As Collection
Dim filename As String

    ' Separate the patterns.
    pattern_array = Split(patterns, ";")

    Set files = New Collection
    ' Loop through the files in the directory.
    filename = Dir$(dirname)
    Do While Len(filename) > 0
        ' See if the name matches any pattern.
        For Each pattern In pattern_array
            If LCase$(filename) Like LCase$(pattern) Then
                files.Add filename
                Exit For
            End If
        Next pattern

        filename = Dir$()

    Set FindFiles = files
End Function

The function splits its patterns parameter into an array of separate
pattern strings. It then uses Dir to loop through the files in the
target directory. For each file, it loops through the patterns to see if
the file's name matches any of the patterns. If a file matches a
pattern, the code adds it to the result collection.
2. New HowTo: Make thumbnails and a web page to display the images in a
directory in Visual Basic 6

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

' 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
            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,
        picThumbnail.Height = Me.ScaleY(shrunk_height, vbPixels,

        ' 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 & """>" & _
    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"
(http://www.vb-helper.com/howto_search_multiple_patterns.html) 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.

Twitter feeds:

Post questions at:
 Previous Message All Messages Next Message 
  Check It Out!

  Topica Channels
 Best of Topica
 Art & Design
 Books, Movies & TV
 Food & Drink
 Health & Fitness
 News & Information
 Personal Finance
 Personal Technology
 Small Business
 Travel & Leisure
 Women & Family

  Start Your Own List!
Email lists are great for debating issues or publishing your views.
Start a List Today!

© 2001 Topica Inc. TFMB
Concerned about privacy? Topica is TrustE certified.
See our Privacy Policy.