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

Just a short one this time.

Have a great week and thanks for subscribing!

Rod
RodSte-@vb-helper.com

Twitter feeds:
    VBHelper
    CSharpHelper
----------
==========

    VB.NET Contents:
1. New HowTo: Search for files that match multiple patterns in Visual
Basic .NET
2. New HowTo: Make thumbnails and a web page to display the images in a
directory in Visual Basic .NET
==========
++++++++++
      <VB.NET>
++++++++++
==========
1. New HowTo: Search for files that match multiple patterns in Visual
Basic .NET
http://www.vb-helper.com/howto_net_get_files_multiple_patterns.html
http://www.vb-helper.com/HowTo/howto_net_get_files_multiple_patterns.zip

The System.IO.Directory.GetFiles method lets you easily search for files
in a directory that match a pattern. Unfortunately it can only search
for files that match a single pattern. For example, if you want to find
files that match the patterns *.bmp, *.gif, *.jpg, and *.png, you're out
of luck.

The FindFiles method shown in the following code searches for files that
match multiple patterns.

' Search for files matching the patterns.
Private Function FindFiles(ByVal dir_name As String, ByVal patterns As
String, ByVal search_subdirectories As Boolean) As List(Of String)
    ' Make the result list.
    Dim files As New List(Of String)()

    ' Get the patterns.
    Dim pattern_array() As String = patterns.Split(";"c)

    ' Search.
    Dim search_option As SearchOption = SearchOption.TopDirectoryOnly
    If search_subdirectories Then search_option =
SearchOption.AllDirectories
    For Each pattern As String In pattern_array
        For Each filename As String In Directory.GetFiles(dir_name,
pattern, search_option)
            If Not files.Contains(filename) Then files.Add(filename)
        Next filename
    Next pattern

    ' Sort.
    files.Sort()

    ' Return the result.
    Return files
End Function

The method creates an output List and then splits its patterns parameter
into an array of separate pattern strings. Then for each pattern, the
code uses Directory.GetFiles to search for files matching the pattern.
If a matching file is not yet in the result list, the code adds it. The
method finishes by sorting and returning the results.
==========
2. New HowTo: Make thumbnails and a web page to display the images in a
directory in Visual Basic .NET
http://www.vb-helper.com/howto_net_thumbnail_web_page.html
http://www.vb-helper.com/HowTo/howto_net_thumbnail_web_page.zip

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 web_page As String, ByVal
thumb_width As Integer, ByVal thumb_height As Integer)
    ' Open the HTML file.
    Dim html_filename As String = output_dir & web_page
    Using html_file As New StreamWriter(html_filename)
        ' Make a list of the image files.
        Dim files As List(Of String) = _
            FindFiles(input_dir, "*.bmp;*.gif;*.jpg;*.png;*.tif", False)

        ' Process the files.
        For Each image_filename As String In files
            ' Copy the file to the destination directory.
            Dim image_fileinfo As New FileInfo(image_filename)
            Dim dest_filename As String = output_dir &
image_fileinfo.Name
            File.Copy(image_filename, dest_filename, True)

            ' Get the image.
            Using bm As New Bitmap(image_filename)
                ' Get the original size.
                Dim src_rect As New Rectangle(0, 0, bm.Width, bm.Height)

                ' Shrink the image.
                Dim scale As Double = Math.Min( _
                    CDbl(thumb_width) / bm.Width, _
                    CDbl(thumb_height) / bm.Height)
                Dim shrunk_width As Integer = CInt(bm.Width * scale)
                Dim shrunk_height As Integer = CInt(bm.Height * scale)
                    dim dest_rect as new Rectangle(0, 0, shrunk_width,
shrunk_height)

                Using thumbnail As New Bitmap(shrunk_width,
shrunk_height)
                    ' Copy the image at reduced scale.
                    Using gr As Graphics = Graphics.FromImage(thumbnail)
                        gr.DrawImage(bm, dest_rect, src_rect,
GraphicsUnit.Pixel)
                    End Using

                    ' Save the thumbnail image.
                    Dim thumb_filename As String = _
                        dest_filename.Substring(0, _
                        dest_filename.Length -
image_fileinfo.Extension.Length) & _
                        "_thumb.png"
                    thumbnail.Save(thumb_filename, ImageFormat.Png)

                    ' Add the thumbnail image to the HTML page.
                    Dim thumb_fileinfo As New FileInfo(thumb_filename)
                    html_file.WriteLine( _
                        "<a href=""" & url_prefix & image_fileinfo.Name
& """>" & _
                        "<img src=""" & url_prefix & thumb_fileinfo.Name
& """>" & _
                        "</a>")
                End Using ' Using thumbnail As New Bitmap(shrunk_width,
shrunk_height)
            End Using ' Using bm As New Bitmap(image_filename)
        Next image_filename

        ' Close the HTML file.
        html_file.Close()

        MessageBox.Show("Processed " & files.Count & " images.")
    End Using ' Using html_file As New StreamWriter(html_filename)
End Sub

The method starts by creating a StreamWriter so it can write the new web
page. Next it calls the FindFiles method to get files in the target
directory that have bmp, gif, jpg, png, or tif extensions. See the
example "Search for files that match multiple patterns in Visual Basic
.NET"
(http://www.vb-helper.com/howto_net_get_files_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 bitmap of that size and copies the original image into
it. The method saves the thumbnail in .png format and finally writes a
line into the web page to display the thumbnail, linking it to the
full-size image.
==========
Archives:
    http://www.topica.com/lists/VBHelper
    http://www.topica.com/lists/VB6Helper
    http://www.topica.com/lists/VBNetHelper

Twitter feeds:
    VBHelper
    CSharpHelper

Post questions at:
    http://www.topica.com/lists/VBHelperQA
	
 Previous Message All Messages Next Message 
  Check It Out!

  Topica Channels
 Best of Topica
 Art & Design
 Books, Movies & TV
 Developers
 Food & Drink
 Health & Fitness
 Internet
 Music
 News & Information
 Personal Finance
 Personal Technology
 Small Business
 Software
 Sports
 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.