Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleQuickly view graphic files scaled to fit the form
Keywordsviewer, graphic files, view files, utilities, scale, fit
CategoriesUtilities
 
By Juha Toivonen.

Use the DriveListBox, DirListBox, and FileListBox controls. When the user selects a new drive, set the DirListBox's path to the new drive. When the user selects a new directory, set the FileListBox's path to the new directory. When the user selects a new file, call subroutine LoadFile to display it.

 
Private Sub DriveList_Change()
    'On Error GoTo DriveError
    DirList.Path = DriveList.Drive
    Exit Sub

DriveError:
    DriveList.Drive = DirList.Path
    Exit Sub
End Sub

Private Sub DirList_Change()
    FileList.Path = DirList.Path
End Sub

' Load the selected file.
Private Sub FileList_Click()
    fName = FileList.Path + "\" + FileList.FileName

    MousePointer = vbHourglass
    DoEvents
    LoadFile
    MousePointer = vbDefault
End Sub
 
The original version of this program (see Quickly view graphic files) uses scroll bars to display pictures that are too large to fit on the form. This version shrinks the image if necessary to make it fit. It displays the picture in an Image control and uses the control's Stretch property to resize the image if necessary.
 
' Load the file, shrinking it if necessary
' to make it fit.
Private Sub LoadFile()
    On Error GoTo LoadPictureError

    Dim multiplierX As Single
    Dim multiplierY As Single
    Dim nHeight As Single
    Dim nWidth As Single

    ' Work with the Image control.
    With FileImage
        .Visible = False
        .Stretch = False
        .Picture = LoadPicture(fName)
        Caption = "Viewer [" & fName & " " & .Width / _
            Screen.TwipsPerPixelX & "x" & .Height / _
            Screen.TwipsPerPixelY & "] "

        ' Make the picture fit.
        nWidth = Me.ScaleWidth - Me.ScaleLeft - _
            Picture1.Width
        nHeight = Me.ScaleHeight - Me.ScaleTop

        multiplierX = nWidth / .Width
        multiplierY = nHeight / .Height

        If multiplierX < 1 Or multiplierY < 1 Or mMaximize _
            Then
            .Stretch = True
            If multiplierX < multiplierY Then
                .Width = .Width * multiplierX
                .Height = .Height * multiplierX
            Else
                .Width = .Width * multiplierY
                .Height = .Height * multiplierY
            End If
        End If

        .Left = Me.ScaleLeft + Picture1.Width + ((nWidth - _
            .Width) / 2)
        .Top = Me.ScaleTop + ((nHeight - .Height) / 2)

        .Visible = True
    End With
    Exit Sub

LoadPictureError:
    Beep
    MousePointer = vbDefault
    Caption = "Viewer [Invalid picture]"
    Exit Sub
End Sub
 
Finally, if the user clicks on the form or picture, the program toggles whether it should enlarge small pictures and redisplays the image.
 
' Toggle whether we enlarge small pictures.
Private Sub Form_Click()
    mMaximize = Not mMaximize
    LoadFile
End Sub
 
This program also displays some eyes that follow the cursor position as you move the mouse. You can get code for this control and 100 others in my book Custom Controls Library.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated