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
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
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..
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…