draw a big image from small ones

I am working on a project to make an scrolling msg board animation. The basic idea is user can input a certain length of message and it will be shown on a scrolling animation.

I have multiple font picture I want to use when the program can recognize what message has been input, it will go to find the corresponding letters and make a message picture.

My question is in VB.NET it has image list control can store the images and can let you call it very intuitive. What’s the solution in XOJO?

For example, in VB.NET following codes are works. What do I need if I want to re-write in XOJO?

Public function makeMsgImage (ByVal msgTxt As String) As Bitmap
    Dim imageW As Integer = 140 * Len(msgTxt)  //set msgImage width
    Dim imageH As Integer = 140   //set msgImage Height
    Dim txtImage As New Bitmap(ImageW, ImageH)   //a new bitmap
    Dim g As Graphics = Graphics.FromImage(txtImage)  //draw on the bitmap
    Dim x As Integer = 0

    For Each c As Char In msgTxt
        Select Case c
            case "A"
                g.DrawImageUnscaled(ImageList1.Images("letterA.jpg"), x, 0)  //draw letter A on txtImage
            case "B"
            ...
            case "Z"
        End Select
        x += 140
    Next
    g.Dispose()
    Return txtImage
End Function

Make ImageList1 a Dictionary and preload it with the images from disk, keyed by the file name.

Public function makeMsgImage (msgTxt As String) As Picture
    Dim imageW As Integer = 140 * Len(msgTxt)  //set msgImage width
    Dim imageH As Integer = 140   //set msgImage Height
    Dim txtImage As New Picture(ImageW, ImageH, 32) 
    Dim g As Graphics = txtImage.Graphics  //draw on the picture
    Dim x As Integer = 0
    Dim i As integer
    Dim c As String

    For i = 1 to Len(msgTxt)
        c = mid(msgTxt, i, 1)
        Select Case c
            case "A"
                g.DrawPicture(ImageList1.Value("letterA.jpg"), x, 0)  //draw letter A on txtImage
            case "B"
            ...
            case "Z"
        End Select
        x = x + 140
    Next
    Return txtImage
End Function

Even better would be to pre-map each image to the letter so

        Select Case c
            case "A"
                g.DrawPicture(ImageList1.Value("letterA.jpg"), x, 0)  //draw letter A on txtImage
            case "B"
            ...
            case "Z"
        End Select

becomes

g.DrawPicture(ImageList1.Value(c), x, 0)

[quote=102201:@Tim Hare]Make ImageList1 a Dictionary and preload it with the images from disk, keyed by the file name.

[code]
Public function makeMsgImage (msgTxt As String) As Picture
Dim imageW As Integer = 140 * Len(msgTxt) //set msgImage width
Dim imageH As Integer = 140 //set msgImage Height
Dim txtImage As New Picture(ImageW, ImageH, 32)
Dim g As Graphics = txtImage.Graphics //draw on the picture
Dim x As Integer = 0
Dim i As integer
Dim c As String

For i = 1 to Len(msgTxt)
    c = mid(msgTxt, i, 1)
    Select Case c
        case "A"
            g.DrawPicture(ImageList1.Value("letterA.jpg"), x, 0)  //draw letter A on txtImage
        case "B"
        ...
        case "Z"
    End Select
    x = x + 140
Next
Return txtImage

End Function
[/code][/quote]

Thanks Tim! When you suggested making a dictionary preload images from disk, do I need to put all images into a folder in my project? I’m asking because I definitely hope those font files are not touchable for end user. In VB.NET, the image list solve this problem, it will pack all the images into the exe file after you build your project.

So in XOJO, I guess I need to do the same thing, correct me if I was wrong. If we put them into some folder under my project, how can I refer the images to dictionary? Following codes I know it will load a picture from disk, but I’m not sure if the picture will be packed into my application file(MAC) or not.

Dim d As New Dictionary
Dim f As FolderItem = GetFolderItem("FolderPath/letterA.jpg")
d.value("A") = f.OpenAsPicture

Accessing them from disk is the most automatable, which is why I suggested it. You can drop the images into your project and they will be “baked in” to the executable and the user can’t touch them. Note that if you put them into a folder, you can drag the entire folder into your project and import them all at once. It also retains a folder structure in your project and keeps everything tidy.

If you drop the file letterA.jpg into your project, it becomes a picture literal named “letterA” (no extension). So to load the dictionary you’d say

d.Value("A") = letterA
d.Value("B") = letterB
etc.