Display image in Report when I only have path?

I’ve got a path to an image, and need to display the image in a report field. Is this possible?

I’m creating a temp thumbnail image that will be deleted upon quitting. To prevent putting a ton of image data into an in-memory database (there could be hundreds of small images), I’m writing a thumbnail to a temp folder on the disk and storing the path to that temp file in the database. I can’t figure out if it’s possible to call up the image though, when generating a report. It seems I can only point to the datafield from the recordset (which contains the path as text, not the image itself).

Is there a way to do this?

Ok, So I decided to try putting the image in a BLOB to test out how bad the memory usage is. However, the following code isn’t working (see below for explanation:

I’m passing mediaItem as Dictionary into this method. It contains a set of key/value pairs for items to be added to the DB.

[code]//set up the record set to insert into the database
dim r as DatabaseRecord
r = new DatabaseRecord

//loop through the dictionary that holds the metadata for this file
//and add the key/value pairs to the record set
for each key As Variant In mediaItem.Keys
dim field as string = key

if field = “mediaFileFirstThumb” or field = “mediaFileMidThumb” or field = “mediaFileLastThumb” then
//First, add the path string to the database using the current key name
r.column(field) = mediaItem.value(key)

//open the image
Dim picFile As FolderItem = GetFolderItem(mediaItem.value(key))

If picFile <> Nil Then
  Dim pic As Picture
  Dim mb As MemoryBlock
  
  pic = Picture.Open(picFile)
  
  if pic <> nil then
    //Put it in the memory block
    mb = pic.GetData(Picture.FormatJPEG, Picture.QualityHigh)
    
    //Alter key name to include IMG at the end, so we can put the image in the DB as a BLOB under this new key name
    dim newKey as string = field + "IMG"
    
    //Add the mb to the database as BLOB
    r.column(newKey) = mb
  else
    msgbox "picture is nil"
  end if
  
  break
End If

else
//This is not a thumbnail, so just add it to the database record set
r.column(field) = mediaItem.value(key)
end if

next
[/code]

The issue is happening in opening the image file. When I get to the break, in the debugger I can see that picFile is correctly populated with the FolderItem for this image. However, pic is nil, so I get the msgbox error. I’m not really sure why it’s nil though, since it’s a valid FolderItem, and the file in question is a simple PNG. I’ve tried with JPG as well, but no luck.

@Perry Paolantonio — I suspect a “memory full” error. If you are on macOS, use Activity Monitor to see how much memory is used/available.

I have always had troubles with loading images in a tight loop because the memory is not purged inside the loop so I always ended with all the images loaded in memory at the same time until there is no memory left.

Hmm. The Mac I’m on has 16GB of RAM and I’m using 10. So there’s plenty available. The images in question are only about 110KB each, so they shouldn’t take up too much – and in this test case, I’m only creating three images total. At worst, the app seems to be using about 40MB of RAM while running.

Honestly, I’d much rather just store a path to the image in the database and then pull up the actual image when generating the final report. i’m using an in-memory database, so with hundreds of thumbnails, it could be an issue to be storing them in BLOBS. But I can’t seem to find a way to have the built-in Xojo reports convert the path to an actual image.

@Perry Paolantonio — Well ReportPicture only handles Pictures, so either way you will need to load the Picture yourself.

Maybe you should check that “picFile.exists” returns true as expected

Aha. Now we’re getting somewhere. testing if picFile.Exists fails, yet I can see the file is there. According to the debugger:

mediaItem.value(key):  (the string I'm passing to GetFolderItem)
/Users/perry/Library/Application\\ Support/Manifest.debug/tmp/87A13A233E8245CFB3D3E1BC07546E66.png

And from picFile:

AbsolutePath: 
iMac HD:Users:perry:Documents:Business:Xojo Projects:/Users/perry/Library/Application\\ Support/Manifest.debug/tmp/3104C56602174421902A378FCD38766B.png

DisplayName: 
/Users/perry/Library/Application\\ Support/Manifest.debug/tmp/3104C56602174421902A378FCD38766B.png

Name: 
/Users/perry/Library/Application\\ Support/Manifest.debug/tmp/3104C56602174421902A378FCD38766B.png

NativePath: 
/Users/perry/Documents/Business/Xojo Projects/:Users:perry:Library:Application\\ Support:Manifest.debug:tmp:3104C56602174421902A378FCD38766B.png

ShellPath: 
/Users/perry/Documents/Business/Xojo\\ Projects/\\:Users\\:perry\\:Library\\:Application\\\\\\ Support\\:Manifest.debug\\:tmp\\:3104C56602174421902A378FCD38766B.png

URLPath:
file:///Users/perry/Documents/Business/Xojo%20Projects/:Users:perry:Library:Application%5C%20Support:Manifest.debug:tmp:3104C56602174421902A378FCD38766B.png

The permissions on the tmp folder I’ve created are allowing my application to generate the thumbnails, and everyone has read-only access (which is all I’m doing here, so I don’t think that’s the problem).

So why would the FolderItem clearly point to a file that exists, yet report that it doesnt?

@Perry Paolantonio — All the paths are wrong! It seems you have actually passed a POSIX path to GetFolderItem without indicating the proper type. If you store the POSIX path, you need to use:

Dim picFile As FolderItem = GetFolderItem(mediaItem.value(key), FolderItem.PathTypeNative)

hah. I think I just figured this out as you were typing. I’m compiling the path to the image file in code and storing that in the database. that’s where mediaItem.value(key) comes from.

I’ve never looked too closely at the path values Xojo is storing, so i assumed that they look this way (doubled-up) because I’m running in a debug environment!

Thanks for helping me puzzle through it!