locked
loop through RRS feed

  • Question

  • User-1941296693 posted

    How do I loop through imagenames

    So say I had 3 images

    image1
    image2
    image3

    How could I loop through these, using something like the code below

    Dim iCounter As Integer
    iCounter = 1

    Do while iCounter<4

    'get image1, image2 and image3 on the loop through using the iCounter value

    iCounter +1

    Loop

     

    thanks.

     

    Thursday, July 22, 2010 6:38 AM

Answers

  • User-952121411 posted

    Yep this is not too difficult to do.  Use generic collection of type 'Image' you can iterate through the Image objects easily, and not have to worry about Do Loops with counts, etc.  Take a look to the code below that does everything from creation to iterating through the collection:

            'Create (3) images from the System.Drawing.Image object
            Dim Image1 As Image = Nothing
            Dim Image2 As Image = Nothing
            Dim Image3 As Image = Nothing
    
            'Load the images from files on disk (or whatever method needed; just an example)
            Image1 = Image.FromFile("C:\MyImage1.jpg")
            Image2 = Image.FromFile("C:\MyImage2.jpg")
            Image3 = Image.FromFile("C:\MyImage3.jpg")
    
            'Create a generic collection of Image objects, and add the images created above
            Dim ImageList As New List(Of Image)
            ImageList.Add(Image1)
            ImageList.Add(Image2)
            ImageList.Add(Image3)
    
            '...Sometime later you want to iterate through the collection of Image objects
            For Each Img As Image In ImageList
    
                'Extract the image properties
                Dim h As Double = Img.Height
                Dim w As Double = Img.Width
    
            Next

    If you are not familiar with working with object collections, take a look to the following for more help:

    .NET Object Collections Using Generics 101: 

    http://allen-conway-dotnet.blogspot.com/2009/11/net-object-collections-using-generics.html

    Hope this helps! Smile 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 22, 2010 11:54 AM

All replies

  • User-1028151323 posted

    Where do you have them? In a directory or in a database?


    Thursday, July 22, 2010 6:58 AM
  • User-952121411 posted

    Yep this is not too difficult to do.  Use generic collection of type 'Image' you can iterate through the Image objects easily, and not have to worry about Do Loops with counts, etc.  Take a look to the code below that does everything from creation to iterating through the collection:

            'Create (3) images from the System.Drawing.Image object
            Dim Image1 As Image = Nothing
            Dim Image2 As Image = Nothing
            Dim Image3 As Image = Nothing
    
            'Load the images from files on disk (or whatever method needed; just an example)
            Image1 = Image.FromFile("C:\MyImage1.jpg")
            Image2 = Image.FromFile("C:\MyImage2.jpg")
            Image3 = Image.FromFile("C:\MyImage3.jpg")
    
            'Create a generic collection of Image objects, and add the images created above
            Dim ImageList As New List(Of Image)
            ImageList.Add(Image1)
            ImageList.Add(Image2)
            ImageList.Add(Image3)
    
            '...Sometime later you want to iterate through the collection of Image objects
            For Each Img As Image In ImageList
    
                'Extract the image properties
                Dim h As Double = Img.Height
                Dim w As Double = Img.Width
    
            Next

    If you are not familiar with working with object collections, take a look to the following for more help:

    .NET Object Collections Using Generics 101: 

    http://allen-conway-dotnet.blogspot.com/2009/11/net-object-collections-using-generics.html

    Hope this helps! Smile 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 22, 2010 11:54 AM