Results 1 to 18 of 18

Thread: loading a sequence of images from a selected folder into a picturebox control...

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    29

    Question loading a sequence of images from a selected folder into a picturebox control...

    hello, i have a simple photo viewer application where i want the user to click a "load sequence" button and then i want something like a folderbroswerdialog to appear then the user can pick a folder with images in it then the images get loaded into the picturebox and the user can click a "forward" button to go to the next image in the sequence and a "backward" button to go to the previous image in the sequence, it's kinda like windows 7 photo viewer.

    Thank you very much guys for your answers

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: loading a sequence of images from a selected folder into a picturebox control...

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim files() As String
    4.     Dim arrayIndex As Integer
    5.  
    6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.         'select folder
    8.         Dim fbd As New FolderBrowserDialog
    9.         fbd.SelectedPath = My.Computer.FileSystem.SpecialDirectories.MyPictures
    10.  
    11.         Dim extensions() As String = {".bmp", ".gif", ".jpg", ".png"}
    12.  
    13.         If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
    14.             arrayIndex = 0
    15.             files = IO.Directory.GetFiles(fbd.SelectedPath).Where(Function(s1) extensions.Any(Function(s2) s2 = IO.Path.GetExtension(s1))).ToArray
    16.             If files.Count > 0 Then
    17.                 PictureBox1.Image = New Bitmap(files(0))
    18.             End If
    19.         End If
    20.     End Sub
    21.  
    22.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    23.         'forward
    24.         If files.Count > arrayIndex + 1 Then
    25.             arrayIndex += 1
    26.             PictureBox1.Image = New Bitmap(files(arrayIndex))
    27.         End If
    28.     End Sub
    29.  
    30.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    31.         'back
    32.         If arrayIndex >= 1 Then
    33.             arrayIndex -= 1
    34.             PictureBox1.Image = New Bitmap(files(arrayIndex))
    35.         End If
    36.     End Sub
    37.  
    38. End Class

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    29

    Re: loading a sequence of images from a selected folder into a picturebox control...

    thank you I'll try it out now

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    29

    Re: loading a sequence of images from a selected folder into a picturebox control...

    wow you're unbelievable it worked straight off the bat! i would like to add you as a friend and ill definitely rate you . i have just one other question, how can i have like a global keyboard shortcut to go to the next photo and previous even when my form isn't the active one e.g. crl+alt+ right arrow/ left arrow

    Thank you

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: loading a sequence of images from a selected folder into a picturebox control...

    for global hotkeys search the forum for RegisterHotkey/UnRegisterHotkey api functions

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    29

    Re: loading a sequence of images from a selected folder into a picturebox control...

    hi again i got the global hotkeys working and all was well but then i tried a folder with 17 images instead of about 5-4 and it jumps around a bit like if you push forward for the first time it will jump about 5 photos and it also won't start with the first photo in the folder. i think what I'm trying to say is that i want the photos to go in a certain order, if it helps all the photos in the folder are named 1-17 or 1-5 e.g. 1.jpg, 2.jpg ect. (they get created by another program) in that way.
    thank you .paul.
    Last edited by happycamper1221; Dec 30th, 2011 at 11:14 PM.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: loading a sequence of images from a selected folder into a picturebox control...

    ok. post your latest code + i'll show you how to sort the files + hopefully stop the jumping

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    29

    Re: loading a sequence of images from a selected folder into a picturebox control...

    I think the code is all the same as last time but here it is:

    vb Code:
    1. Dim files() As String
    2. Dim arrayIndex As Integer
    3.  
    4.  
    5.    Private Sub LoadSequenceToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles LoadSequenceToolStripMenuItem.Click
    6.  
    7.         'select folder
    8.         Dim fbd As New FolderBrowserDialog
    9.         fbd.SelectedPath = My.Computer.FileSystem.SpecialDirectories.MyPictures
    10.         Dim extensions() As String = {".bmp", ".gif", ".jpg", ".png"}
    11.         If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
    12.             arrayIndex = 0
    13.             files = IO.Directory.GetFiles(fbd.SelectedPath).Where(Function(s1) extensions.Any(Function(s2) s2 = IO.Path.GetExtension(s1))).ToArray
    14.  
    15.             If files.Count > 0 Then
    16.                 PictureBox1.Image = New Bitmap(files(0))
    17.             End If
    18.         End If
    19.  
    20.     End Sub
    21.  
    22.  
    23. 'forward
    24.         If files.Count > arrayIndex + 1 Then
    25.             arrayIndex += 1
    26.             PictureBox1.Image = New Bitmap(files(arrayIndex))
    27.         End If
    28.  
    29. 'back
    30.         If arrayIndex >= 1 Then
    31.             arrayIndex -= 1
    32.             PictureBox1.Image = New Bitmap(files(arrayIndex))
    33.         End If

    it just seems to go in whatever order it wants and jumps around all over the place which wont work with what I'm trying to design. I also noticed that we're in a bit of a time difference because your in the UK and I'm in Australia, right now it's 12:24pm in aus lol i don't know what time it is in the UK but hopefully you can reply as soon as possible. thanks
    Last edited by happycamper1221; Dec 31st, 2011 at 08:27 PM. Reason: forgot something

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: loading a sequence of images from a selected folder into a picturebox control...

    sorry for the delay. as you said we're 11 hours behind Sydney here + i slept in as it's New Years Day

    vb Code:
    1. Private Sub LoadSequenceToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadSequenceToolStripMenuItem.Click
    2.     'select folder
    3.     Dim fbd As New FolderBrowserDialog
    4.     fbd.SelectedPath = My.Computer.FileSystem.SpecialDirectories.MyPictures
    5.     Dim extensions() As String = {".bmp", ".gif", ".jpg", ".png"}
    6.     If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
    7.         arrayIndex = 0
    8.         files = IO.Directory.GetFiles(fbd.SelectedPath).Where(Function(s1) extensions.Any(Function(s2) s2 = IO.Path.GetExtension(s1))).ToArray
    9.  
    10.         Try
    11.             Array.Sort(files, Function(x, y) CInt(IO.Path.GetFileNameWithoutExtension(x)).CompareTo(CInt(IO.Path.GetFileNameWithoutExtension(y))))
    12.         Catch ex As Exception
    13.  
    14.         End Try
    15.  
    16.         If files.Count > 0 Then
    17.             PictureBox1.Image = New Bitmap(files(0))
    18.         End If
    19.     End If
    20. End Sub

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    29

    Re: loading a sequence of images from a selected folder into a picturebox control...

    Okay that's awesome it works! now there one more thing i need if i may.

    okay so what i'm aiming to do is have three picture boxes stacked under each other and i want the first picturebox "picturebox1" to have 1 image before picturebox2 and i want picturebox2 to have one image before picturebox3, so i want picturebox three to have the latest image and picturebox2 to have the middle image and picturebox1 to have the oldest image and when you click the forward and backward buttons they all change accordingly. it might be hard to understand but here's the minor code changes i did to get a semi working effect that jumps to many images instead of just having three images in the three pictureboxes.

    vb Code:
    1. Private Sub LoadSequenceToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles LoadSequenceToolStripMenuItem.Click
    2.  
    3.         'select folder
    4.         Dim fbd As New FolderBrowserDialog
    5.         fbd.SelectedPath = My.Computer.FileSystem.SpecialDirectories.MyPictures
    6.         Dim extensions() As String = {".bmp", ".gif", ".jpg", ".png"}
    7.         If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
    8.             arrayIndex = 0
    9.             files = IO.Directory.GetFiles(fbd.SelectedPath).Where(Function(s1) extensions.Any(Function(s2) s2 = IO.Path.GetExtension(s1))).ToArray
    10.             Try
    11.                 Array.Sort(files, Function(x, y) CInt(IO.Path.GetFileNameWithoutExtension(x)).CompareTo(CInt(IO.Path.GetFileNameWithoutExtension(y))))
    12.             Catch ex As Exception
    13.             End Try
    14.             If files.Count > 0 Then
    15.                 PictureBox1.Image = New Bitmap(files(0))
    16.                 PictureBox2.Image = New Bitmap(files(0))
    17.                 PictureBox3.Image = New Bitmap(files(0))
    18.             End If
    19.         End If
    20.  
    21.     End Sub
    22.  
    23.  'forward
    24.         If files.Count > arrayIndex + 1 Then
    25.             arrayIndex += 1
    26.             PictureBox1.Image = New Bitmap(files(arrayIndex))
    27.         End If
    28.  
    29.         If files.Count > arrayIndex + 2 Then
    30.             arrayIndex += 2
    31.             PictureBox2.Image = New Bitmap(files(arrayIndex))
    32.         End If
    33.  
    34.         If files.Count > arrayIndex + 3 Then
    35.             arrayIndex += 3
    36.             PictureBox3.Image = New Bitmap(files(arrayIndex))
    37.         End If
    38.  
    39.  
    40.         'back
    41.         If arrayIndex >= 1 Then
    42.             arrayIndex -= 1
    43.             PictureBox1.Image = New Bitmap(files(arrayIndex))
    44.         End If
    45.  
    46.         If arrayIndex >= 2 Then
    47.             arrayIndex -= 2
    48.             PictureBox2.Image = New Bitmap(files(arrayIndex))
    49.         End If
    50.  
    51.         If arrayIndex >= 3 Then
    52.             arrayIndex -= 3
    53.             PictureBox3.Image = New Bitmap(files(arrayIndex))
    54.         End If

    pretty simple minded i know but it's pretty much the best i could do, i'm just really happy i have your brilliant help for the time being and i'm very curious to know how someone can get to your level of vb.net skill i've been doing vb on and off for a little while now and i can't seem to even remember those large amounts of code, anyways thank you very much Paul i'm very grateful. i'll be looking forward to your next post

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: loading a sequence of images from a selected folder into a picturebox control...

    try this. i didn't thoroughly test it + it might need tweaking if your selected folder has less than 3 picture files:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim files() As String
    4.     Dim arrayIndex As Integer
    5.  
    6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.         'select folder
    8.         Dim fbd As New FolderBrowserDialog
    9.         fbd.SelectedPath = My.Computer.FileSystem.SpecialDirectories.MyPictures
    10.         Dim extensions() As String = {".bmp", ".gif", ".jpg", ".png"}
    11.         If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
    12.             arrayIndex = 0
    13.             files = IO.Directory.GetFiles(fbd.SelectedPath).Where(Function(s1) extensions.Any(Function(s2) s2 = IO.Path.GetExtension(s1))).ToArray
    14.             Try
    15.                 Array.Sort(files, Function(x, y) CInt(IO.Path.GetFileNameWithoutExtension(x)).CompareTo(CInt(IO.Path.GetFileNameWithoutExtension(y))))
    16.             Catch ex As Exception
    17.             End Try
    18.             PictureBox1.Image = If(files.Count >= 1, New Bitmap(files(0)), Nothing)
    19.             PictureBox2.Image = If(files.Count >= 2, New Bitmap(files(1)), Nothing)
    20.             PictureBox3.Image = If(files.Count >= 3, New Bitmap(files(2)), Nothing)
    21.         End If
    22.     End Sub
    23.  
    24.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    25.         'back
    26.         If arrayIndex >= 1 Then
    27.             arrayIndex -= 1
    28.             PictureBox1.Image = If(files.Count >= arrayIndex, New Bitmap(files(arrayIndex)), Nothing)
    29.             PictureBox2.Image = If(files.Count >= arrayIndex + 1, New Bitmap(files(arrayIndex + 1)), Nothing)
    30.             PictureBox3.Image = If(files.Count >= arrayIndex + 3, New Bitmap(files(arrayIndex + 2)), Nothing)
    31.         End If
    32.     End Sub
    33.  
    34.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    35.         'forward
    36.         If files.Count > arrayIndex + 3 Then
    37.             arrayIndex += 1
    38.             PictureBox1.Image = If(files.Count >= arrayIndex, New Bitmap(files(arrayIndex)), Nothing)
    39.             PictureBox2.Image = If(files.Count >= arrayIndex + 1, New Bitmap(files(arrayIndex + 1)), Nothing)
    40.             PictureBox3.Image = If(files.Count >= arrayIndex + 3, New Bitmap(files(arrayIndex + 2)), Nothing)
    41.         End If
    42.     End Sub
    43.  
    44. End Class

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    29

    Re: loading a sequence of images from a selected folder into a picturebox control...

    okay this ones probably an easy one but say if i have a menustrip button that says new window and i want it to open another from1 like having too form ones open and you can just keep on opening new ones how would i do that, i've seen it done before and i already tried goggling it. Thanks

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: loading a sequence of images from a selected folder into a picturebox control...

    Quote Originally Posted by happycamper1221 View Post
    i'm very curious to know how someone can get to your level of vb.net skill i've been doing vb on and off for a little while now and i can't seem to even remember those large amounts of code
    i've been programming since about 1992.
    you'll remember more as you go forward + build on your experience.

    have a look at this:

    www.homeandlearn.co.uk

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: loading a sequence of images from a selected folder into a picturebox control...

    Quote Originally Posted by happycamper1221 View Post
    okay this ones probably an easy one but say if i have a menustrip button that says new window and i want it to open another from1 like having too form ones open and you can just keep on opening new ones how would i do that, i've seen it done before and i already tried goggling it. Thanks
    here's how. i used a standard button, but you know how to use a menustrip button already.

    in your vb IDE, select Project-->Add Windows Form to add Form2, then:

    vb Code:
    1. Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    2.     Dim frm As New form2
    3.     frm.show()
    4. End Sub

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    29

    Re: loading a sequence of images from a selected folder into a picturebox control...

    hey again, would there be a simple way to add a fading transition effect between image changes? if so how would i do it.

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: loading a sequence of images from a selected folder into a picturebox control...

    here's an example that fades in/out the picturebox.image, so it alternately shows the picturebox.backgroundimage/picturebox.image:

    http://www.scproject.biz/fadeImage.zip

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    29

    Re: loading a sequence of images from a selected folder into a picturebox control...

    okay so i have this code working :
    vb Code:
    1. Imports System.Drawing.Imaging
    2.  
    3. Public Class Form1
    4.  
    5.     Dim fadeImage As Bitmap
    6.     Dim alpha As Decimal = 1D
    7.     Dim increment As Decimal = -0.1D
    8.  
    9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    10.         Timer1.Enabled = True
    11.  
    12.         If alpha = 1D Then
    13.             increment = -0.05D
    14.  
    15.         End If
    16.     End Sub
    17.  
    18.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    19.         Dim img As New Bitmap("Penguins.jpg")
    20.         PictureBox1.Image = img
    21.  
    22.         img = New Bitmap("Desert.jpg")
    23.         PictureBox1.BackgroundImage = img
    24.  
    25.         fadeImage = DirectCast(PictureBox1.Image.Clone, Bitmap)
    26.     End Sub
    27.  
    28.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    29.         alpha = CDec(alpha + increment)
    30.  
    31.         Dim image_attr As New ImageAttributes
    32.         Dim cm As ColorMatrix
    33.  
    34.         Dim bm As New Bitmap(fadeImage.Width, fadeImage.Height)
    35.         Dim gr As Graphics = Graphics.FromImage(bm)
    36.         Dim rect As Rectangle = _
    37.             Rectangle.Round(fadeImage.GetBounds(GraphicsUnit.Pixel))
    38.  
    39.         cm = New ColorMatrix(New Single()() { _
    40.             New Single() {1.0, 0.0, 0.0, 0.0, 0.0}, _
    41.             New Single() {0.0, 1.0, 0.0, 0.0, 0.0}, _
    42.             New Single() {0.0, 0.0, 1.0, 0.0, 0.0}, _
    43.             New Single() {0.0, 0.0, 0.0, 0.0, 0.0}, _
    44.             New Single() {0.0, 0.0, 0.0, CSng(alpha), 1.0}})
    45.         image_attr.SetColorMatrix(cm)
    46.         gr.DrawImage(fadeImage, rect, 0, 0, fadeImage.Width, fadeImage.Height, _
    47.             GraphicsUnit.Pixel, image_attr)
    48.  
    49.         PictureBox1.Image = bm
    50.  
    51.         If alpha = 0D Then
    52.             increment = 0.05D
    53.             Timer1.Enabled = False
    54.         ElseIf alpha = 1D Then
    55.             'i got rid of the increment thing here because it wouldn't do anything when i changed it
    56.             Timer1.Enabled = False
    57.         End If
    58.  
    59.     End Sub
    60.  
    61. End Class

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    29

    Re: loading a sequence of images from a selected folder into a picturebox control...

    okay so i have this code working :
    vb Code:
    1. Imports System.Drawing.Imaging
    2.  
    3. Public Class Form1
    4.  
    5.     Dim fadeImage As Bitmap
    6.     Dim alpha As Decimal = 1D
    7.     Dim increment As Decimal = -0.1D
    8.  
    9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    10.         Timer1.Enabled = True
    11.         ' i added a new increment code here so i could change the increment both ways.
    12.         If alpha = 1D Then
    13.             increment = -0.05D
    14.  
    15.         End If
    16.     End Sub
    17.  
    18.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    19.         Dim img As New Bitmap("Penguins.jpg")
    20.         PictureBox1.Image = img
    21.  
    22.         img = New Bitmap("Desert.jpg")
    23.         PictureBox1.BackgroundImage = img
    24.  
    25.         fadeImage = DirectCast(PictureBox1.Image.Clone, Bitmap)
    26.     End Sub
    27.  
    28.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    29.         alpha = CDec(alpha + increment)
    30.  
    31.         Dim image_attr As New ImageAttributes
    32.         Dim cm As ColorMatrix
    33.  
    34.         Dim bm As New Bitmap(fadeImage.Width, fadeImage.Height)
    35.         Dim gr As Graphics = Graphics.FromImage(bm)
    36.         Dim rect As Rectangle = _
    37.             Rectangle.Round(fadeImage.GetBounds(GraphicsUnit.Pixel))
    38.  
    39.         cm = New ColorMatrix(New Single()() { _
    40.             New Single() {1.0, 0.0, 0.0, 0.0, 0.0}, _
    41.             New Single() {0.0, 1.0, 0.0, 0.0, 0.0}, _
    42.             New Single() {0.0, 0.0, 1.0, 0.0, 0.0}, _
    43.             New Single() {0.0, 0.0, 0.0, 0.0, 0.0}, _
    44.             New Single() {0.0, 0.0, 0.0, CSng(alpha), 1.0}})
    45.         image_attr.SetColorMatrix(cm)
    46.         gr.DrawImage(fadeImage, rect, 0, 0, fadeImage.Width, fadeImage.Height, _
    47.             GraphicsUnit.Pixel, image_attr)
    48.  
    49.         PictureBox1.Image = bm
    50.  
    51.         If alpha = 0D Then
    52.             increment = 0.05D
    53.             Timer1.Enabled = False
    54.         ElseIf alpha = 1D Then
    55.             'i got rid of the increment thing here because it wouldn't do anything when i changed it.
    56.             Timer1.Enabled = False
    57.         End If
    58.  
    59.     End Sub
    60.  
    61. End Class

    and what would be fantastic is if i could have some sort of class or separate file that i could just import in the one place and just reference to for this fade so for example which i have no idea what I'm typing:

    vb Code:
    1. Private Sub PictureBox2_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox2.Click
    2.  
    3. 'maybe there's some variables declared like "fademe" and "img1", "img2"
    4.  
    5. fademe.images = img1,amg2
    6.  
    7. img1 = my.resources.img1
    8. img2 = my.resources.img2
    9.  
    10. fademe.intervalin = 0.5
    11. fademe.intervalout = 0.5
    12.  
    13. "ect... no idea - but i think i just invented a new programming language :p.
    14.  
    15. End Sub

    so if you see what i mean (if it's possible) how could i go about doing this. once again thank you so so much you are my Savior .

Tags for this Thread

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