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
|