Image drop issue

I have canvas accepting image drops however I ran into to some issues.
I was testing it out and all is good until I drop a text file on it and it crashed with no error message. This happened while running from the IDE.

I only want the canvas to accept images, this has me a bit puzzled.

Here is code from the canvas open event -

var pngType As New FileType
pngType.Name = "image/png"
pngType.Extensions = "png"

Var jpegType As New FileType
jpegType.Name = "image/jpeg"
jpegType.Extensions = "jpg;jpeg"

Var gifType As New FileType
gifType.Name = "image/gif"
gifType.Extensions = "gif"

Var pictType As New FileType
pictType.Name = "image/pict"
pictType.Extensions = "pict;pict2"

Var bmpType As New FileType
bmpType.Name = "image/bmp"
bmpType.Extensions = "bmp"

Var tiffType As New FileType
tiffType.Name = "image/tiff"
tiffType.Extensions = "tiff;tif"

Var heicType As New FileType
heicType.Name = "image/heic"
heicType.Extensions = "heic"

Var pdfType As New FileType
pdfType.Name = "image/pdf"
pdfType.Extensions = "pdf"

Var psdType As New FileType
psdType.Name = "image/psd"
psdType.Extensions = "psd;PSD"

Var tgaType As New FileType
tgaType.Name = "image/tga"
tgaType.Extensions = "tga"


me.AcceptFileDrop(pngType+ jpegType + gifType+pictType+bmpType+tiffType+heicType+pdfType+psdType+tgaType)

Another issue is if I just use me.AcceptFileDrop(pngType) it still accepts other image formats and other files types even if I remove the other file types.

Then in the DropObject

 if Obj.FolderItemAvailable then
  Var fld_item As New FolderItem(obj.FolderItem.NativePath)
  var p as Picture
  
  If fld_item <> Nil Then
    p = Picture.Open(fld_item)
    sourceImages.Add(p)
   ImageCanvas.Refresh
  End If
  
end if

Xojo 2022 r3.2
Mac OS 13.7.6

Thanks.

Try the same code with a newer version, what’s the behavior? The same?

If you use AcceptFileDrop you need to filter your FileDrop Types, otherwise it will try to process text files and crash, as you found. You can use Select Case for the file types you want. If it doesn’t match, it gets ignored. Using the System.DebugLog(a) line gives you filetype strings you can use to filter in the Select Case

If obj.FolderItem <> Nil Then
  Var a As String = obj.FolderItem.Type
  
  System.DebugLog(a)
  
  
  Select Case obj.FolderItem.Type
    
  Case "PNGimage", "JPEGimage", "image/gif", "image/pict", "image/bmp", "image/tiff", "image/heic", "image/pdf", "AdobePhotoshopfile", "image/tga"
    Var p As picture = picture.open(obj.FolderItem)
    Me.Backdrop = p
  End Select
  
End If

Tried in 2025 r1. Same result.

I thought setting the fileTypes in the opening event was suppose limit what was suppose to accepted for drops.

I’ll try that code out.

1 Like

Me too. Else what is it for?

A few try…catch constructs wouldnt hurt here too.

ok I tried the System.DebugLog(a) thing. Well in the message log, no types shown. I checked (a) and it was blank. as well as obj.FolderItem.Type.

Puzzling…

It’s supposed to, but I found that this:

var pngType As New FileType
pngType.Name = "image/png"
pngType.Extensions = "png"

me.AcceptFileDrop(pngType)

was not accepting png files when testing. That’s why I added the debug line, and got “PNGimage” when I dropped a png file on it.

Yeah I’m a bit puzzled here

Have to look in to try/catch thing.

if I just do this

Var jpegType As New FileType
jpegType.Name = "image/jpeg"
jpegType.Extensions = "jpg;jpeg"

me.Accept(jpegType)

It will still allow png and other type drops..

This works ok on Windows. But I needed to change the ephemeral var to static or the accept stopped as soon as Opening() got out of scope.

drop.zip (4.4 KB)

I think Mac is broken, because Windows or do it right or don’t accept files at all (using Var).

Intersting.

Oddly enough the fileType thing works fine when using it with OpenFileDialog for filtering, at least on Mac. However using it for AcceptFileDrop.. not so much..

Puzzling..

What’s the behavior of my sample running in the IDE in your system?
Does it work properly too?
I can’t drag a .txt file here.

Using your example in macOS 15.5 and Xojo 2025r1.1 it accepts all file types and doesn’t stop any drops.

I’ve never actually been able to make that respect file types, but all of my apps are macOS only.

Mac is broken. Windows works.

Later I’ll get my Mac and see it by myself.

Tried your sample in xojo 2022 r3.2 and 2025 r1 it still accepts all drops.

Yep seems so.

A “work around” for now is, I could use the Split(“.”) on the file name and get the extension then if any extension is of whatever text type, ignore them or if any extension is not equal to whatever image types, just ignore them…

Kind of annoying but if it works.. :thinking: :laughing:

I “believe” I can do better, just can’t right now. :wink:

In Brazil we call your need as a “go horse” or “GOP. gambiarra oriented programming”. :laughing:

It looks like that the obj type is empty when using a not registered one, so this may help us like

drop.xojo_binary_project.zip (4.9 KB)

1 Like

Xojo has FolderItem.Extension since a few months already, so you don’t need splitting.