dictionary of images

  1. 4 months ago

    Hello,
    I'm trying to build a dictionary of pictures (imagesets), but I can't understand how to pass/cast the pictures to it.

    So, let us say I have n imagesets, and a constant of their names (pictList):

    dim splitter(-1) as string = split(pictlist, EndOfLine)

    pictDict = new Dictionary//it is a property

    for i as integer = 0 to splitter.ubound
    dim s as String = splitter(i)
    pictDict.Value(s) = //how to reference the picture or imageset named s?
    next

    Suggestions appreciated. Thanks.

  2. Mike C

    Jan 4 Pre-Release Testers, Xojo Pro North Carolina (USA)
    Edited 4 months ago

    How many images do you see being inside of each "imageset"?

    This is a way you could also tackle your question.

    You might want to create a class with a picture, name, and maybe some other properties. You could keep an array of these classes and then call what you need from that. This will give you a "box (class)" to have a unique Picture, name, and potentially other properties.

    For example create a class that has a picture, and a few example properties:
    [-image-

    (on the fly code)

    // INDIVIDUAL CLASSES UNIQUE FOR EACH PICTURE
    Dim x as Integer // This is how many Image Sets you need
    
    for i as Integer = 0 to x
    
    Dim c as New adapterName_Class // Instantiate new class to use
    imagesetClass.manuPic = somePicture // load your image to this class
    imagesetClass.deviceName = "ImageName" // name your image
    
    
    // CREATE THE ARRAY TO HOLD ALL OF THESE SPECIFIC CLASSES YOU CREATED
    Dim imagesetClassArr() as imagesetClass // YOU WILL MOST LIKELY WANT TO MAKE THIS A GLOBAL PROPERTY TO AVOID SCOPE ISSUES
    
    // LOAD YOUR UNIQUE CLASS TO THE ARRAY
    imagesetClassArr.Append (imagesetClass)
    
    
    // RINSE AND REPEAT
    Next i
    

    Then a light example to draw what you have loaded in your imagesetClassArr Array for Canvas.Paint event.

    for x as Integer = 0 to imagesetClassArr.Ubound
    
    // DRAW PIC
    Dim thisImage as Picture = imagesetClassArr(x).manuPic
    g.drawpicture(thisImage, 0,0 thisImage.width, thisImage.height)
    // DRAW NAME
    Dim thisImageName as String = imagesetClassArr(x).deviceName
    g.drawstring(thisImageName, x, y, w)
    Next x
    

    Hopefully this make sense, but if not please let us know.

    Thanks and HTH.
    Mike

  3. Hi Mike,
    thank you for answering. But what I'm after, if possible, is to detect all the pictures available in a project.
    All this years I relied on matching the content of Resources folder with the hardcoded name of each picture, as in the snippets below.
    Now I was wondering if there was a more efficient way to detect all the picture in a project and bypass the returnPct function.

    dim splitter(-1) as string = split(pictlist, EndOfLine)
    pictDict = new Dictionary//it is a property

    for i as integer = 0 to splitter.ubound
    dim s as String = splitter(i)
    pictDict.Value(s) = returnPct(s)
    next

    Public Function returnPct(s as string) as Picture
    //sourceFolder refers to the Resources folder of the app
      dim f as FolderItem = sourceFolder.Child(s + ".png")
      if f <> nil then
        if f.Exists then
          return picture.open(f)
        end if
      end if
    End Function
  4. @Carlo R — Well, the definition of efficiency really depends on your project. For most projects, loading pictures only when they are needed is the best option. Although, in your specific case, you may need to preload the pictures for extra responsiveness if the memory footprint remains reasonable. It is up to you and it will not make coding any different.

  5. Edited 4 months ago

    @Stéphane Mons
    Food for thought. I'll see how responsive is my app loading pictures only when needed.
    Thanks.

  6. Mike C

    Jan 5 Pre-Release Testers, Xojo Pro North Carolina (USA)
    Edited 4 months ago

    It would be more effective and expedient to store the location (native path) of said images instead of the images themselves.

  7. 3 months ago

    Indeed. Sometimes I wonder what makes me so dense as not to see the most obvious way.
    Thank you.

or Sign Up to reply!