Canvas , images and more

Hello, I need to create a dynamic image viewer. I can scroll through the images back and forth. I have the path of the image in a string called PathImmagine, how do I handle it? I would like to view it with Canvas but I don’t know how to do it. I’m tested canvas1.Backdrop=PathImmagine but return an error.

  1. Make sure you have a valid (existing, accessible, …) FolderItem for your Image.
  2. Read the content of the FolderItem into a Picture Object
  3. Draw the Picture Object into the Canvas (resize it at the same time if needed)

https://documentation.xojo.com/api/files/folderitem.html
https://documentation.xojo.com/api/graphics/picture.html#picture-open
https://documentation.xojo.com/api/user_interface/desktop/desktopcanvas.html

You need to load the picture from the path.

Var myPic as picture
Var f As FolderItem
f = New FolderItem(PathImmagine)
If f.exists and f <> nil then
myPic.open(f)
Canvas1.Backdrop = myPic
end if
1 Like

Wouldn’t it be better to test for NIL first?

If f <> NIL And f.Exists Then

1 Like

and then

myPic.open(f)

If myPic <> Nil Then
    Canvas1.Backdrop = myPic
Else
    // Picture couldn't be loaded from file...
End If

You can in this case add code to create a Picture at the fight size,
add a Rectangle (0,0,WIdth, Height)
add two diagonal lines
draw a “Missing Picture” information string (in red ?)
Add this Picture to Canvas1.Backdrop

Of course, this is not mandatory and is not meant to be displayed, but if by unluck the image is not there, the user knows an information is missing…

Something like this (the SQLIte Logo Is not found / I changed its name to get the error drawing):

The text is not centered, but can be…

Testing for nil is indeed preferable first.

In case f is nil then testing for exists will trigger an exception.

1 Like

How do you do that ? If in a ListBox, you’d better store the images folder item (I suppose they are stored in HD and you display only the images names)…

Return an error:

You must use the value returned by this function
myPic.Open(f)

myPic = Picture.Open(f) :wink:

Please use the Documentation. All the needed Docs are already linked in the 1st reply to your Thread.:slight_smile:

2 Likes

all the examples show: f = FolderItem.ShowOpenFileDialog(“image/jpeg”) I need to assign a PATH value read from the table and which I compose first as a string in a PathImmagine variable. I can’t figure out how to do it. I come from VB6 and many things are complicated for me. I tested the code

Var p As Picture
Var f As FolderItem
f = FolderItem.ShowOpenFileDialog("image/jpeg")
If f <> Nil Then
  p = Picture.Open(f)
  Canvas1.Backdrop = p
End If

and this is OK…

But if i tested this code

[code]

Var f As FolderItem
f = New FolderItem(PathImmagine)
Var p As Picture
If f <> Nil Then
p = Picture.Open(f)
Canvas1.Backdrop = p
End If
[/code] i have errors …

I replaced the dynamic PATH with a fixed PATH and it works … the problem I discovered, it is in the Path that it reads, which is not complete, even if it should be complete … it does not insert the last letter, I also tried to change his name and does the same. how can i solve?

We can‘t say because we can‘t see how appPath is created. :wink:

Ok i’m solved:
Var appPath As String = App.ExecutableFile.NativePath
appPath = appPath.mid(1,appPath.Length - 24)

i changed -25 in -24 and i have correct path.

No good idea. If the path changes, it would break.

Take a look at the various FolderItem Path Properties and NthField f.e., to find a better solution. I am not at my Dev.Desk and can‘t help with Snippets.

Here is one (very inefficient but easy to understand) way to get the Path and Filename from the NativePath Property of a FolderItem:

Var fiFolderItem As FolderItem = FolderItem.ShowOpenFileDialog("*.*")

Var strPath(-1) As String
Var strPathResult As String

strPath = fiFolderItem.NativePath.ToArray("\")

For X As Integer = 0 To strPath.Count-2
  
  strPathResult = strPathResult + strPath(X) + "\"
  
Next

MessageDialog.Show("Native Path: " + fiFolderItem.NativePath + EndOfLine + _
"Filename: " + fiFolderItem.Name + EndOfLine + _
"Path w/o Filename: " + strPathResult)

Remember, you can rightclick on a command in the Xojo Editor, to lookup a description in the Xojo Manual. :wink:

In fact, removing a character is not a good idea. In my program it can change the Path but not the names and the location of the folders within the root folder. I can also retrieve the path with: Var f As FolderItem Dim PathDelProgramma as string f = GetFolderItem("")

Or

Just use App.ExecutableFile

1 Like