Canvas Images

I am new to xojo and have already familiarized myself with a small project. Currently I have encountered a problem where I unfortunately don’t know what to do next.

The problem: In my desktop app I download an image from an URL and then want to load it into a specific canvas. There are about 20 different canvas in my project. This is because i want to load different images into different canvas.

When I now assign the image using MainCanvas.backdrop, unfortunately no image appears.
The following Code works, when i open the FileDialog an select the image.

// Get filename from URL
Var con as New URLConnection

Var url As String = "https://MyUrlToMyPicture.jpg"
Var filenameLength As Integer
For i As Integer = url.Length - 1 DownTo 0
  If url.Middle(i, 1) = "/" Then
    filenameLength = url.Length - i
    Exit For
  End If

Var filename As String
filename = url.Right(filenameLength)

Var downloadFile As FolderItem = SpecialFolder.Desktop.Child(filename)
con.Send("GET", url, downloadFile)

Var picFile As FolderItem
picFile = FolderItem.ShowOpenFileDialog("")

If picFile <> Nil And picFile.Exists Then
  Var pic As Picture
  pic = Picture.Open(picFile)
  MainWindow.MainCanvas.Backdrop = pic
End If

// This works NOT
.... same code as above ......

Var picFile As FolderItem
picFile = downloadFile

.... same code as above ......

I don’t get it. Why does the picture not loading into my canvas?
Can please point me someone into the right direction please?
Thank you in advanced.

send is Asynchronously
you can try SendSync
or drop this URLConnection component into your window.

validate the download first and then try to view it.

Thanks Markus for your reply.

I tried SendSync now and it is working!!! :slight_smile:

I thought, the condition is already validating the download…
In this case Im abviously wrong.

If picFile <> Nil And picFile.Exists Then
End If

It does validate, but think about what it does if there is no file …

The code doesn’t wait for a file to download and is done before an answer is even received.