School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,029 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,210 people online right now. Registration is fast and FREE... Join Now!




The Image list

 
Reply to this topicStart new topic

> The Image list, Using a component to make a slideshow.

Asscotte
Group Icon



post 19 May, 2009 - 06:55 AM
Post #1


Hello,

Recently I learned What you can do with an image list and now I wish to share this knowledge with you.

First Put;
- One image list component onto your form
- One picture box.
- One Timer (optinal)
- Two Buttons
- One Textbox

Once you have done that place the picture box in the center of your form and the buttons and the textbox at the bottom. Make sure one button is next to the textbox name it "set location" and the other "next".

First we need to enter some functions:
CODE

    Private allowedExtensions() As String = { _
    ".gif", _
    ".jpg", _
    ".bmp", _
    ".png", _
    ".tiff" _
        }
    Function fillImlWithFilesFromDir(ByRef dirPath As String) As ImageList

        Dim dirFiles() As String = IO.Directory.GetFiles(dirPath)




        For Each dirFile As String In dirFiles
            For Each extension As String In allowedExtensions
                If extension = IO.Path.GetExtension(dirFile) Then
                    ImageList1.Images.Add(Image.FromFile(dirFile))
                End If
            Next
        Next

        Return ImageList1
    End Function




Baisicly this bit of code:
CODE

    Private allowedExtensions() As String = { _
    ".gif", _
    ".jpg", _
    ".bmp", _
    ".png", _
    ".tiff" _
        }


sets the allowed extensions so when we set the target folder to get the images from we don't want a word document a text file and a HTML document finding their way in there do we ph34r.gif .

This bit of code:
CODE

Function fillImlWithFilesFromDir(ByRef dirPath As String) As ImageList

        Dim dirFiles() As String = IO.Directory.GetFiles(dirPath)




        For Each dirFile As String In dirFiles
            For Each extension As String In allowedExtensions
                If extension = IO.Path.GetExtension(dirFile) Then
                    ImageList1.Images.Add(Image.FromFile(dirFile))
                End If
            Next
        Next

        Return ImageList1
    End Function


is the function we will use to populate the image list component. And once the file has been found (if it conforms to the above extensions then it gets added to the image list.

for you set location button enter the code:
CODE

   ImageList1 = fillImlWithFilesFromDir(TextBox1.Text)


this basically calls the function we created earlier and sets the target directory as textbox1.text now I would also enclose this statement in an try catch block to prevent crashing. so the code would look like this:
CODE

        Try
            ImageList1 = fillImlWithFilesFromDir(TextBox1.Text)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


now for the next button add this code:
CODE

        Static imageNumber As Integer = 0
        imageNumber = (imageNumber + 1) Mod ImageList1.Images.Count
        PictureBox1.Image = ImageList1.Images(imageNumber)
        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

What this code does is first create a number "imageNumber" then uses that to add one to the image number on the image list, this bit here:
CODE

PictureBox1.Image = ImageList1.Images(imageNumber)


is the actual putting the image into the picture box and this section of code:
CODE

PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

makes sure the picture box doesnt expand with the image or crop the image.


And there you go. If you wanted to create a slideshow then you would create another button and use that to start the timer which has this code in it:
CODE

        Static imageNumber As Integer = 0
        imageNumber = (imageNumber + 1) Mod ImageList1.Images.Count
        PictureBox1.Image = ImageList1.Images(imageNumber)
        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage


of course you could add some nice fading effects - but that's another tutorial biggrin.gif .

Enjoy,
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 08:25AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month