Question : Loading images from directory into 5 Pictureboxes HOW?

Ok I posted an image so you can get the idea of what I am doing.

  My images of vehicles are in their respective folders for example

2005_Ford_Mustang

The files contained in the above folder would be

2005_Ford_Mustang_001.jpg
2005_Ford_Mustang_002.jpg
2005_Ford_Mustang_003.jpg
2005_Ford_Mustang_004.jpg      and so on

As you can see in the image of the app ... I have one large PictureBox with 4 smaller underneath named as follows

Large PB = pbMain
smaller 4 = pb1, pb2, pb3, pb4
3 buttons  = btnLoad, btnPrev, btnNext


INFO:
 Each directory contains images from 1 to as many as 200

NEED:
 I need to quickly load the images in the lower 4 PB's
    for example: If only 1 image in DIR then pb1 would have an image and pb2 - pb4 are blank (black color so as to dissapear into the back ground)

However  Lets say there are 4 or more images in directory
    Example: The first 4 images are loaded into the small PB's pb1 -pb4
                    pb1 has focus and thus is displayed in pbMain

USER CONTROL:
      With the images loaded user can now:
1. PRESS on any of the smaller PB's pb1 - pb4 and that image will be displayed in pbMain
2. Or user could PRESS Buttons btnPrev (PREVIOUS) btnNext (NEXT) and cycle through images in the directory

NOTE: btnLOad loads the images from directory via a variable called myPath which points to the directory


Answer : Loading images from directory into 5 Pictureboxes HOW?

That's correct.  You could just add the controls to your form and use the names I have in the code.

At a minimum, it requires...

Labels: lblImageNumber
Buttons: btnLoad, btnPrev, btnNext, btnPrevPage, btnNextPage
PictureBoxes: pbMain, pbThumb1, pbThumb2, pbThumb3, pbThumb4

Then the code would look like below instead.
*Note that I'm still loading up the "thumbs" List in the Load event.

I also changed the btnLoad() click event so it calls LoadJPEGs() instead of loading the files directly.  This way you can just call LoadJPEGs() with any path...hopefully you can see how that would be used.  ;)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
Public Class Form1

    Private CurIndex As Integer
    Private CurPageIndex As Integer
    Private ImagePaths As New List(Of String)
    Private thumbs As New List(Of PictureBox)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        thumbs.Add(pbThumb1)
        thumbs.Add(pbThumb2)
        thumbs.Add(pbThumb3)
        thumbs.Add(pbThumb4)
    End Sub

    Private Sub btnLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        Using fbd As New FolderBrowserDialog
            If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
                LoadJPEGs(fbd.SelectedPath)
            End If
        End Using
    End Sub

    Private Sub LoadJPEGs(ByVal path As String)
        ImagePaths.Clear()
        ImagePaths.AddRange(My.Computer.FileSystem.GetFiles(path, FileIO.SearchOption.SearchTopLevelOnly, "*.jpg").ToArray)
        btnPrevPage.Enabled = (ImagePaths.Count > 4)
        btnNextPage.Enabled = (ImagePaths.Count > 4)
        For i As Integer = 1 To 4
            thumbs(i - 1).Visible = (ImagePaths.Count >= i)
        Next
        CurIndex = 0
        CurPageIndex = 0
        If ImagePaths.Count > 0 Then
            DisplayThumbs(0)
            thumb_Click(pbThumb1, Nothing)
        Else
            MessageBox.Show("No .JPGs present")
        End If
    End Sub

    Private Sub DisplayThumbs(ByVal startIndex As Integer)
        If ImagePaths.Count > 0 Then
            CurPageIndex = Math.Max(0, Math.Min(ImagePaths.Count - 4, startIndex))
            For i As Integer = 0 To 3
                If CurPageIndex + i < ImagePaths.Count Then
                    Try
                        Using fs As New System.IO.FileStream(ImagePaths(CurPageIndex + i), IO.FileMode.Open)
                            thumbs(i).Image = Image.FromStream(fs)
                        End Using
                    Catch ex As Exception
                        MessageBox.Show("File: " & ImagePaths(CurPageIndex + i) & vbCrLf & vbCrLf & ex.ToString, "Error Opening Image", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
                End If
            Next
        End If
    End Sub

    Private Sub thumb_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pbThumb1.Click, pbThumb2.Click, pbThumb3.Click, pbThumb4.Click
        Dim pb As PictureBox = CType(sender, PictureBox)
        If Not IsNothing(pb.Image) Then
            pbMain.Image = pb.Image
            CurIndex = CurPageIndex + thumbs.IndexOf(pb)
            lblImageNumber.Text = (CurIndex + 1) & " of " & ImagePaths.Count
        End If
    End Sub

    Private Sub btnPages_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevPage.Click, btnNextPage.Click
        DisplayThumbs(CurPageIndex + IIf(sender Is btnPrevPage, -4, 4))
    End Sub

    Private Sub btnPrevNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrev.Click, btnNext.Click
        Dim target As Integer = CurIndex + IIf(sender Is btnPrev, -1, 1)
        If target >= 0 AndAlso target < ImagePaths.Count Then
            Try
                Using fs As New System.IO.FileStream(ImagePaths(target), IO.FileMode.Open)
                    pbMain.Image = Image.FromStream(fs)
                End Using
                CurIndex = target
                lblImageNumber.Text = (CurIndex + 1) & " of " & ImagePaths.Count
                If target < CurPageIndex OrElse target > (CurPageIndex + 3) Then
                    DisplayThumbs(CurIndex)
                End If
            Catch ex As Exception
                MessageBox.Show("File: " & ImagePaths(target) & vbCrLf & vbCrLf & ex.ToString, "Error Opening Image", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub

End Class
Random Solutions  
 
programming4us programming4us