ImageLooper Control

I am working on a VB tutorial, and at the same time trying to create my first control called ImageLooper. This is pretty straightforward, a control box with a picturebox1 inside, a left arrow button and a right arrow button (button1 and button1 respectively). The idea is, with each click in either direction, the next or previous in line image will display, depending on which arrow is clicked.

Each button works individually ok, but when I switch from the left arrow button to the right or visa versa, instead of going to the next image down or up, on the first click, it takes one step in the opposite direction, displays that (wrong) image, then, with subsequent clicks, starts moving through the images correctly. The language is below:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

PictureBox1.Image = System.Drawing.Image.FromFile _

("E:\vb05sbs\chap07\face0" & Counter & ".ico")

Counter += 1

If Counter = 5 Then Counter = 1

 End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

PictureBox1.Image = System.Drawing.Image.FromFile _

("E:\vb05sbs\chap07\face0" & Counter & ".ico")

Counter -= 1

If Counter = 0 Then Counter = 4

End Sub

The referenced "face0" images are happy face images from sad to very happy and are named face01, face02, face03, face04. they are provided with the tutorial to give you something to work with, but it could be anything like that which can be incremented in number.

There is this functionality in the winfax viewer, where you can right click open that image in the viewer and from that initial image you can then click a right arrow and loop through everything in the folder. If it reaches the end it just starts over, which this code does. But that is what I am trying to achieve with making this control.

The other questions on this would be, what if you don't want to name a specific folder or file, but have it be a folder or starting file selected from a common dialog, or a selected filename from a listbox list

Thanks in advance...




Answer this question

ImageLooper Control

  • shawn087

    When (which statement, which file, ...) are you getting this error This kind of error can sometimes be caused when loading a file, which is not a correct image file (e.g. you try to load a text file as an image...)

    Andrej



  • Darren France

    Ok - Interesting. There were no other "filetypes" in the folder per se but there was a thumbnail view .db, the one created by windows when you use thumb view So I deleted that and no more error message... and it did loop perfectly. Which leads to the question - is there a way, perhaps with a "try...catch" type statement, to have the program skip over an invalid filetype This would be a little tricky perhaps since we create an array from the folder content...

  • markm75c

    Andrej Tozon wrote:

    Hi,

    here's somewhat complete code, see if it helps:

    Andrej

    Thanks for this code, I am trying it out and will post results. This is a very good approach to what I am ultimately trying to accomplish....

    Tarey



  • prujohn

    Hi,

    here's somewhat complete code, see if it helps:

    class-level variable declarations:

    Private photos() As String
    Private currentPhoto As Integer

    Rest of the code:

    Private Sub ChooseFolderButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChooseFolderButton.Click
       
    Using dialog As New
    FolderBrowserDialog()
           
    If dialog.ShowDialog() <> Windows.Forms.DialogResult.OK
    Then
               
    Return
           
    End
    If
           
    photos = System.IO.Directory.GetFiles(dialog.SelectedPath, "*.*"
    )
        End Using
       
    If photos.Length > 0
    Then
           
    currentPhoto = 0
            DisplayPhoto()
       
    End
    If
    End Sub

    Private Sub LeftButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LeftButton.Click
        currentPhoto -= 1
       
    If currentPhoto < 0
    Then
           
    currentPhoto = photos.GetUpperBound(0)
       
    End
    If
       
    DisplayPhoto()
    End Sub

    Private Sub RightButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RightButton.Click
        currentPhoto += 1
       
    If currentPhoto > photos.GetUpperBound(0)
    Then
           
    currentPhoto = 0
       
    End
    If
        DisplayPhoto()
    End Sub

    Private Sub DisplayPhoto()
        PictureBox1.Image = Image.FromFile(photos(currentPhoto))
    End Sub

    Andrej



  • kiran1234

    Andrej Tozon wrote:

    Private Sub DisplayPhoto()
    PictureBox1.Image = Image.FromFile(photos(currentPhoto))
    End Sub

    Andrej

    Hi Andrej - I have been working with this code and for some reason I keep getting a 'Name Image Is Not Declared' Error on this "Image.FromFile" syntax.

    It doesn't make sense to me. I tried importing some references, nothing seems to remove the error.... Any ideas why this might be doing that Thanks....



  • WXS123

    Hi again,

    Image class resides in the System.Drawing.dll and is available through System.Drawing namespace. Make sure your project references System.Drawing.dll and you 'import' System.Drawing namespace [or access this class through System.Drawing.Image.FromFile(...)).

    Andrej



  • pschla

    I guess we crossed responses... yes, that is what it was. The drawing reference. Works great!

  • JFLund

    When you get an error on this line - can you check the photos(currentPhoto) array item and make sure it points to a valid file

    Andrej



  • drajwani

    Check my code in this thread for a way to filter for valid image files.


  • Antarctica

    I think I answered my own question. Below is the revised code which seems to work well. I think I read someplace that an image can't be called directly but has to be called as a bitmap, which you can then work with directly. I'm new at this but this shed a lot of light as I studied it to make it work... Thanks again.

    Imports System

    Imports System.Collections

    Imports System.ComponentModel

    Imports System.Threading

    Imports System.Windows.Forms

    Imports System.Windows.Forms.PictureBox

    Public Class FileViewer

    'class-level variable declarations:

    Private photos() As String

    Private currentPhoto As Integer

    Private Image As Drawing.Bitmap

    'Rest of the code:

    Private Sub ChooseFolderButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChooseFolderButton.Click

    Using dialog As New FolderBrowserDialog()

    If dialog.ShowDialog() <> DialogResult.OK Then

    Return

    End If

    photos = System.IO.Directory.GetFiles(dialog.SelectedPath, "*.*")

    End Using

    If photos.Length > 0 Then

    currentPhoto = 0

    DisplayPhoto()

    End If

    End Sub

    Private Sub LeftButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LeftButton.Click

    currentPhoto -= 1

    If currentPhoto < 0 Then

    currentPhoto = photos.GetUpperBound(0)

    End If

    DisplayPhoto()

    End Sub

    Private Sub RightButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RightButton.Click

    currentPhoto += 1

    If currentPhoto > photos.GetUpperBound(0) Then

    currentPhoto = 0

    End If

    DisplayPhoto()

    End Sub

    Public Sub DisplayPhoto()

    PictureBox1.Image = Drawing.Image.FromFile(photos(currentPhoto))

    End Sub

    End Class



  • Igor Nikiforov

    You change the counter *after* displaying the image. The Counter value thus doesn't match the image being displayed. Easy fix: change the counter before displaying the image.


  • Keyu

    nobugz wrote:
    You change the counter *after* displaying the image. The Counter value thus doesn't match the image being displayed. Easy fix: change the counter before displaying the image.

    This answers the question of the glitch with the existing code, and helps me see what it was - thanks a lot :)



  • alpefusk

    Hi - yes, I am getting an "outofmemory exception" on this line when I get to the end of the collection of files in the folder, and if I click another time (going in either direction):

    PictureBox1.Image = Drawing.Image.FromFile(photos(currentPhoto))

    I see that the language is there to loop but for some reason it isn't looping. It works great up until I get to each end of the array.



  • .net sukbir

     Tarey Wolf wrote:

    IPrivate Sub LeftButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LeftButton.Click

    currentPhoto -= 1

    If currentPhoto < 0 Then

    currentPhoto = photos.GetUpperBound(0)

    End If

    DisplayPhoto()

    End Sub

    Private Sub RightButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RightButton.Click

    currentPhoto += 1

    If currentPhoto > photos.GetUpperBound(0) Then

    currentPhoto = 0

    End If

    I did run into something with the array which I am sure is simple in that, in the original exercise, once the user clicked "past" the beginning or ending item in the array, it looped to either the last item if moving to the left or to the first item if moving to the right...  Now I get an "out of memory" error message meaning I think that I've run out of array slots. I think I would like it to loop back to the upperbound (or lower - depending on direction one is clicking)... how would I adapt that to this syntax



  • ImageLooper Control