Results 1 to 5 of 5

Thread: Picture Viewer Question

  1. #1

    Thread Starter
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Picture Viewer Question

    I started redesigning a picture viewer (of my grandchildren) after someone had asked a question about loading images. So I could distribute this app to friends and relatives I decided the best way to do it was to include a folder to be installed on their C: This way I could update the folder of pictures at anytime without changing the application. Right now I select either individual names or all the pictures from a combo box like this:

    vb Code:
    1. Private Sub cboSelectPicture_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSelectPicture.SelectedIndexChanged
    2.       Me.Refresh()
    3.       Try
    4.           Dim selectedName As String = Me.cboSelectPicture.Text
    5.           ' Get and display pictures of only grandchild selected
    6.           If selectedName <> "All Grandchildren" Then
    7.               Dim imageFiles As String() = IO.Directory.GetFiles("C:\Pictures\", selectedName & "*.jpg")
    8.               Dim images As New ArrayList
    9.               For Each imageFile As String In imageFiles
    10.                     Select Case IO.Path.GetExtension(imageFile).ToLower()
    11.                         Case ".jpg", ".bmp", ".gif", ".png"
    12.                             images.Add(Image.FromFile(imageFile))
    13.                     End Select
    14.               Next imageFile
    15.               Me.images = DirectCast(images.ToArray(GetType(Image)), Image())
    16.           ElseIf selectedName = "All Grandchildren" Then
    17.               ' Get and display pictures of all grandchildren
    18.               Dim imageFiles As String() = IO.Directory.GetFiles("C:\Pictures\", "*.jpg")
    19.               Dim images As New ArrayList
    20.               For Each imageFile As String In imageFiles
    21.                   Select Case IO.Path.GetExtension(imageFile).ToLower()
    22.                       Case ".jpg", ".bmp", ".gif", ".png"
    23.                             images.Add(Image.FromFile(imageFile))
    24.                   End Select
    25.               Next imageFile
    26.               Me.images = DirectCast(images.ToArray(GetType(Image)), Image())
    27.           End If
    28.       Catch ex As Exception
    29.           MessageBox.Show("cboSelectPicture_SelectedIndexChanged: " & ex.ToString)
    30.       End Try
    31. End Sub

    Is there a more effective way of doing this or is this as good as it gets?
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

  2. #2
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: Picture Viewer Question

    It will be better if you don't hardcode anything like "All Grandchildren" or "C:\Pictures\". If you are planning to distribute this application then your friends may not be having Grandchildren or may not be having C drive in the hard disk.

  3. #3
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Picture Viewer Question

    1. It would be better if you use List(Of Image) type instead of ArrayList (no casting would be needed)
    2. Also use My.Computer.FileSystem.SpecialDirectories.Desktop & "Image\" & imageName (where "Image" is the folder)
    3. Also, I would shorten the code since most of them in the If statement are the same.

  4. #4
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Picture Viewer Question

    I can’t figure out why you use Case statement since you only request .jpg files. Am I missing something?

  5. #5

    Thread Starter
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: Picture Viewer Question

    Deepak. All Grandchildren comes from a text file in the pictures folder. It also contains all the names of the grandchildren, this way the list can be added to and the combo box will add the new names. Remember these are pictures of my grandchildren. I originally had the pictures in separate folders for each child, and could fill the combo box with the folder names, but could not incorporate "All Grandchildren" into the datasource for the combo box.

    VBDT. I got this code from searching the forum, and it was one of the xamples I came up with. I have a sample with List(Of Image), I will try that instead. The select case was in the original code, I guess I can remove it since I only plan on using jpgs.

    Thanks for your comments.
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width