Results 1 to 14 of 14

Thread: [RESOLVED] imageName = ImageList1.Images.Keys(x)

  1. #1

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    Resolved [RESOLVED] imageName = ImageList1.Images.Keys(x)

    How do I get the image name from an ImageList after using OpenFileDialog to load the images in to the image list.
    The images are loaded in to the image list and from the ImageList to the PictureBoxes but when I try to get the names using
    the last two lines of code Nothing.
    If placed in the ImageList when in design editor then all works fine.
    I did look in the Docs and there looked a really good example to download and work through but it doesn't work.
    So here I am again.
    Code:
                    Dim b As Bitmap = DirectCast(ImageList1.Images(x), Bitmap) 'Get number in Imagelist
                    DirectCast(Me.Controls("pbxFrame" & i), PictureBox).Image = b 'Place images in PictureBoxes
                    imageName = ImageList1.Images.Keys(x) 'Get image name from ImageList
                    savedNames(i) = imageName 'Place image names in an array
                    i += 1
    Learning is a never ending subject.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: imageName = ImageList1.Images.Keys(x)

    Did you actually set a name when you added the images? The names are not part of the image objects themselves. It is completely up to you whether you provide names and what they are. They are simply dictionary keys. If you don't set them, they don't exist. If you're unsure, show us the code that populates the ImageList.

  3. #3

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    Re: imageName = ImageList1.Images.Keys(x)

    Load Images into ImageList
    Code:
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            ImageList1.Images.Clear()
    
            oFD1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
            oFD1.Title = "Open Image Files"
            oFD1.Filter = "Jpeg|*.jpg|Gif|*.gif|Png|*.png|Bmp|*.bmp"
            oFD1.Multiselect = True
    
            ListView1.LargeImageList = ImageList1
    
            If oFD1.ShowDialog() = DialogResult.OK Then
                Dim counter As Integer = 0
                For Each Singlefile As String In oFD1.FileNames
                    ImageList1.Images.Add(Image.FromFile(Singlefile))
                    ListView1.BeginUpdate()
                    ListView1.Items.Add(Singlefile, counter)
                    ListView1.EndUpdate()
                    counter += 1
                Next
            End If
            AssignImagesToSquares()
        End Sub
    Load images from ImageList to PictureBoxes and place names of Images to savedNames array to later compare for a match
    Code:
        Private Sub AssignImagesToSquares()
            Dim imageName As String
            Dim i As Integer = 1
            For j = 0 To 1 'Go through twice 8 images x 2 = 16 squares
                Dim indices() As Integer = Enumerable.Range(0, 8).OrderBy(Function(x) r.NextDouble).ToArray
                For Each x As Integer In indices
                    Dim b As Bitmap = DirectCast(ImageList1.Images(x), Bitmap) 'Get number in Imagelist
                    DirectCast(Me.Controls("pbxFrame" & i), PictureBox).Image = b 'Place images in PictureBoxes
                    imageName = ImageList1.Images.Keys(x) 'Get image name from ImageList
                    savedNames(i) = imageName 'Place image names in an array
                    i += 1
                Next
            Next
        End Sub
    Learning is a never ending subject.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: imageName = ImageList1.Images.Keys(x)

    As I said then, you're not adding a name when you add the image:
    vb.net Code:
    1. ImageList1.Images.Add(Image.FromFile(Singlefile))
    If you expect that image to have a name then you have to provide one when you add it. That Add method is overloaded and the overload that you want is this one:

    https://docs.microsoft.com/en-au/dot...Drawing_Image_

    It's up to you to decide what you want the name of that image to be. If, for instance, you want to use the file name, you might do this:
    vb.net Code:
    1. ImageList1.Images.Add(Path.GetFileNameWithoutExtension(Singlefile), Image.FromFile(Singlefile))
    By the way, calling BeginUpdate and EndUpdate inside the loop that adds the items is useless. The whole point is that you call them when you begin updating and when you end updating, so before and after the loop respectively.

  5. #5

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    Re: imageName = ImageList1.Images.Keys(x)

    Thanks John but can't get either of those suggestions to work Path has a squiggle and not sure how to declare the path even though it is in the singlefile and adding a key value just couldn't figure that at all.
    I think it is time for me to end my vb adventure as everything I try seems to come back to the forum.
    Learning is a never ending subject.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: imageName = ImageList1.Images.Keys(x)

    If you search for "GetFileNameWithoutExtension" you'll see that it is a member of the Path class in the System.IO namespace. If you want to use any type in code, you need to either qualify the type name with the namespace where you use it or else import the namespace, either at the file level or the project level. The System namespace is already imported at the project level so that means that you either use IO.Path in that code or you can import the System.IO namespace.

    Singlefile is the path of the file. The Path class contains methods for manipulating file and folder paths. GetFileNameWithoutExtension is a method that, when provided a file path, will return the name of the file without the extension.

  7. #7

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    Re: imageName = ImageList1.Images.Keys(x)

    Still didn't work for me? clearly I'm missing something which is obvious to yourself.
    Code:
       ImageList1.Images.Add(GetFileNameWithoutExtension(Singlefile), Image.FromFile(Singlefile))
    Learning is a never ending subject.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: imageName = ImageList1.Images.Keys(x)

    Quote Originally Posted by Mallard8 View Post
    Still didn't work for me? clearly I'm missing something which is obvious to yourself.
    Code:
       ImageList1.Images.Add(GetFileNameWithoutExtension(Singlefile), Image.FromFile(Singlefile))
    That's not what I said. Either qualify the type name:
    vb.net Code:
    1. ImageList1.Images.Add(IO.Path.GetFileNameWithoutExtension(Singlefile), Image.FromFile(Singlefile))
    or import the namespace:
    vb.net Code:
    1. Imports System.IO
    and then use the type name unqualified:
    vb.net Code:
    1. ImageList1.Images.Add(Path.GetFileNameWithoutExtension(Singlefile), Image.FromFile(Singlefile))
    The import code I showed goes at the top of the code file to import for just that file, or you can forgo the code and import on the References page of the project properties by checking the namespace in the list. If you do the latter, you will be presented with a list of all the namespaces with member types declared in the current assembly and any referenced assemblies and you can see those that are imported by default, e.g. System.Data and System.Windows.Forms. That's how you can use the DataTable class and all the WinForms control types without issue. Every type is a member of a namespace and every type you use must be disambiguated one way or another. For instance, if you import two namespaces and use the type name that exists in both, you have no choice but to qualify the type name in place in order to specify which of the two types you actually mean.

    I suggest that you follow the Blog link in my signature below and read my post on Assemblies & Namespaces.

  9. #9

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    Re: imageName = ImageList1.Images.Keys(x)

    Code:
    Imports System.IO
    Did that
    Code:
    ImageList1.Images.Add(Path.GetFileNameWithoutExtension(Singlefile), Image.FromFile(Singlefile))
    And That
    But still did not work

    |Followed the example in the Docs and ended up with ths
    Code:
     For Each Singlefile As String In oFD1.FileNames
                    Dim fileName As String = Singlefile
                    result = io.Path.GetFileNameWithoutExtension(fileName)
                    ImageList1.Images.Add(result)
                    ListView1.Items.Add(Singlefile, counter)
                    counter += 1
                Next
    Which did work but can't load the file to the imagelist beacause it has no extension.
    Learning is a never ending subject.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: imageName = ImageList1.Images.Keys(x)

    Quote Originally Posted by Mallard8 View Post
    But still did not work
    What exactly does that mean? Compilation error? Run-time exception? Unexpected behaviour?

  11. #11

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    Re: imageName = ImageList1.Images.Keys(x)

    John,
    First an apology (But still did not work) pointless statement, a trap I don't normally fall in to.
    Second, forgot to follow the advice on a previous thread take a break, Frequently and regularly.
    Finally figured it out I had, and not sure how, Imports System.IO.Path, after spending time away in the garden came back tonight and spotted what had happened, using System.Imports.IO now and all is fine.
    Thanks for having the patience to keep with me on this thread .
    Learning is a never ending subject.

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: imageName = ImageList1.Images.Keys(x)

    Simples...

    Code:
    For Each Singlefile As String In oFD1.FileNames
        Dim fileName As String = io.Path.GetFileNameWithoutExtension(Singlefile)
        ImageList1.Images.Add(fileName, Image.FromFile(Singlefile))
        ListView1.Items.Add(Singlefile, counter)
        counter += 1
    Next

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: imageName = ImageList1.Images.Keys(x)

    Simples...

    Code:
    For Each Singlefile As String In oFD1.FileNames
        Dim fileName As String = io.Path.GetFileNameWithoutExtension(Singlefile)
        ImageList1.Images.Add(fileName, Image.FromFile(Singlefile))
        ListView1.Items.Add(Singlefile, counter)
        counter += 1
    Next

  14. #14

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    Re: imageName = ImageList1.Images.Keys(x)

    Thanks Paul,
    After all that it turned out to be simples !

    I was nearly there with what I had.
    Last edited by Mallard8; Apr 30th, 2020 at 04:05 PM. Reason: Added to reply
    Learning is a never ending subject.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width