FolderItem to open pic files and use them in WebImageViewer?

I am trying to open a PNG file to display into a WebImageViewer.

I’ve tried to use FolderItem to no avail.

How would I resolve this? Do I have to manually add all the pics to my project? There are seventeen choices of pics and I want the user’s five chosen pics to show up in a row of WebImageViewers.

You could try

Var pic As New WebPicture

Var f As FolderItem = New FolderItem(<Path to file>)

Var b As BinaryStream = BinaryStream.Open(f)

pic.Data = b.Read(b.Length)

b.Close

me.Picture = pic

This is in the Shown Event Handler of the ImageViewer.

1 Like

WebFile has an Open shared method that allows you to pass a FolderItem and receive a WebFile, which you can then use in your WebImageViewer.
https://documentation.xojo.com/api/web/webfile.html#webfile-open

1 Like

I tried the following code in an attempt to get this to work:

Var f As FolderItem = New FolderItem("1.png") // Get the file using a FolderItem
If f <> Nil And f.Exists Then
// Convert the FolderItem to a WebFile
App.MyFile = WebFile.Open(f) // MyFile is a property on the App object
App.MyFile.MIMEType = "PNG"
Me.Picture = App.MyFile
End If

Not sure how to get WebFile to work, as there is no sample code on how to convert a WebFile to a WebPicture.

This works beautifully, thanks!

[EDITED] — for this solution to work - I used a Build Step whereby the files and folders are COPIED over into where I intend to draw the pic from.

For NESTED folder items - the following code is what I used, in conjunction with a Build Step to copy over the folder containing my pics:

Var pic As New WebPicture

Var f As FolderItem = New FolderItem("")
f = f.Child("0").Child("1.png")

Var b As BinaryStream = BinaryStream.Open(f)

pic.Data = b.Read(b.Length)

b.Close

me.Picture = pic

Hopefully this adds a layer of context to the original solution.
Thanks again @Wayne_Golding

You can assign the URL property on the WebFile to the URL property of the WebImageView. Be sure to keep the WebFile in scope.

1 Like

I think I am just going to use the solution as-is, as there are literally hundreds of pictures that are pieces of this gameboard I am building.

I’m posting a reply here if someone in the future is looking for a solution. The original question from Amy was in regard to a PNG.

It should be noted that there is a bug in WebImageViewer in opening JPG files, using this same manner. [Feedback: #50612 - webpicture does not load all JPEG]

My workaround was:

Var fiImageFile As FolderItem
Var oImage As Picture
Var oSizedImage As Picture

fiImageFile = NEW FolderItem("c:\Image\AE01-0500.jpg")
oImage = Picture.Open(fiImageFile)
' If the image size is greater than the allowed imgThumbImage size then resize the image to fit (imgThumbImage is my WebImageViewer object) 
If ((oImage.Width > imgThumbImage.Width) Or (oImage.Height > imgThumbImage.Height)) Then
    oSizedImage = NEW Picture(imgThumbImage.Width, imgThumbImage.Height, 32)
    oSizedImage.Graphics.DrawPicture(oImage, 0, 0, imgThumbImage.Width, imgThumbImage.Height, 0, 0, oImage.Width, oImage.Height)
    imgThumbImage.Picture = oSizedImage
Else
    imgThumbImage.Picture = oImage
End If

great!
simple, clear direct
thanks