Create Image Slide Show in

Play Slide show in
Play Slide show in
58.0 KiB

This article is the extension of the past article How to crate Image Thumbnails in The article shows how to create a user control for viewing all images of the directory or folder in a thumbnail view). In this article we extend one more feature that is to play slid show. User can play the slide slow of the images of the folder.
I am creating a Windows Form and Picturebox control at run time for playing slide show, so you have also a opportunity to learn how to create control at run time and how to use them.

You can download the initial project from here(the user who have not VS 2010, can create your own project in your VS version).
First we declare some objects and variables on the form1:

Private IsFullScreen As Boolean = False
    Private ImageIndex As Integer = -1
    Private ImageDir As IO.FileInfo()
    Private frm As Form
    Private FullSizePic As PictureBox

When user select the root folder of image files. The user control load all images of the folder in thumbnail view ( See the my previous post)
I am adding a new Button in the bottom of the form ‘Play Slide Show’.
Create a click procedure of the button and write the code for creating the form and PictureBox control at runtime:

    Private Sub btnSlideShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSlideShow.Click
        If TextBox1.Text <> "" Then
            Dim di As New IO.DirectoryInfo(TextBox1.Text)
            ImageDir = di.GetFiles("*.jpg").Concat(di.GetFiles("*.bmp")).Concat(di.GetFiles("*.png")).Concat(di.GetFiles("*.gif")).ToArray
            Dim dra As IO.FileInfo
            frm = New Form
            frm.Name = "frm"
            FullSizePic = New PictureBox
            FullSizePic.Dock = DockStyle.Fill
            FullSizePic.BackColor = Color.Black
            FullSizePic.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
            AddHandler frm.KeyDown, AddressOf frm_keydown
            Timer1.Enabled = True
        End If
    End Sub

The following function get the image path from the ImageDir (directory fileinfo object) according to next image index and show the image in the picture box:

    Private Sub GetnextImage()
        If Not ImageDir Is Nothing Then
            If ImageIndex < ImageDir.Length - 1 Then
                ImageIndex += 1
                FullSizePic.ImageLocation = ImageDir(ImageIndex).FullName
            ElseIf ImageIndex = ImageDir.Length - 1 Then
                ImageIndex = 0
                FullSizePic.ImageLocation = ImageDir(ImageIndex).FullName
            End If
        End If
    End Sub

The following fucntion can be used to exit the slide show:

    Public Sub LeaveFullScreen(ByVal targetForm As Form)
        If IsFullScreen Then
            ' Restore the original Window state.
           ' Uncomment the code if you does not want to close the slide show.
            'targetForm.WindowState = FormWindowState.Normal
            ' targetForm.FormBorderStyle = FormBorderStyle.None
            ' targetForm.TopMost = topMost
            'targetForm.Bounds = bounds
            IsFullScreen = False
            Timer1.Enabled = False
        End If
    End Sub

Code for showing form in full screen:

    Public Sub EnterFullScreen(ByVal targetForm As Form)
        If Not IsFullScreen Then
            targetForm.WindowState = FormWindowState.Maximized
            targetForm.FormBorderStyle = FormBorderStyle.None
            targetForm.TopMost = True
            targetForm.Bounds = Screen.GetBounds(targetForm)
            IsFullScreen = True
        End If
    End Sub

Write the GetnextImage() procedure on the Timer1_Tick event, after every 5 second the event will be called.

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    End Sub

the following code for exit the slide show at the any key press event:

    Private Sub frm_keydown()
    End Sub

You can download full source code from above.