Drag, Drop & Copy Image To A Folder


I am trying to be able to drag and drop an image from a file to a canvas and then save that image to a specified folder. I have the drag and drop working okay, but I can’t figure out how to actually copy and save the file to a specific folder. The code I am messing with is listed below.

//Drag and drop - This works
If obj.PictureAvailable Then
Me.Backdrop = obj.Picture
ElseIf obj.FolderItemAvailable Then
Me.Backdrop = Picture.Open(obj.FolderItem)
End If

//Trying to save file
Dim imageData As String
Dim f As FolderItem
If Canvas1.Backdrop <> Nil Then
// Get a temporary file to save the image to
If Picture.IsExportFormatSupported(Picture.FormatJPEG) Then
f = getfolderitem("").child(“Pictures”).child(ft.name)

  // Save the image out to the file
  Canvas1.Backdrop.Save(f, Picture.SaveAsJPEG)
End If

End If[/code]

It does not work (of course) I was wondering if someone can give me some advice how I can do this. Any help would be greatly appreciated.


while not use CopyFileTo method in folderitem class to copy?

There are several things to look at.

Is Canvas1.Backdrop nil?
Is Picture.FormatJPEG supported? On Windows, you must turn on GDI+
Is there a Pictures folder next to the executable? In Debug mode, there may not be.
Does ft.name contain a valid filename?

Hi Tim and Christian,

Thank you for taking the time to help me. I finally figured it out using the CopyFileTo method. The code is listed below.

To answer Tim. I am using jpgs. I did turn on the GDI+ on, although I think it was already working when I tested the code with Windows. The folder I wanted to copy the file into (called “Inventory”) was next to the executable and that was the code I was having problem with. If you see any problems with the code I posted, I would appreciate knowing about it, but for now it seems to work.

[code] Dim ft as new filetype
Dim f as FolderItem = obj.FolderItem
Dim Tool as String = f.DisplayName

//Drops the file on canvas
If obj.PictureAvailable Then
Me.Backdrop = obj.Picture
ElseIf obj.FolderItemAvailable Then
Me.Backdrop = Picture.Open(obj.FolderItem)
End If

//Show File Name

//Copies file to target location

Thank you again for your help.


One problem, one improvement.
Your code assumes a folderitem drop. It will break on obj.PictureAvailable. In that case, f will be nil and you will get an exception. For obj.PictureAvailable, you need to use Picture.Save and turn on GDI+

An improvement:


can be written simply as




can be shortened to


It requires one less folderitem object to be created and destroyed.

Thanks Tim for the improvement.

As far as the problem, I’m not sure why it is a problem. In the app, the user drags an image on to the canvas and the image is displayed on the canvas. At the same time, the image is copied to the Inventory folder, and the file name will be saved to a sqlite database. When the record is accessed from the database, the image will be loaded on to the canvas from its saved location in the Inventory folder.

So I’m not sure where it will break the code since the only way for the user to load the image is to drag it from a folder.
Also, I am not sure how to use the Picture.Save. and what that does if the CopyFileTo method also saves the file.

Then why the test of PictureAvailable?

Good question. I guess, I just got this from the example code project. Looks like I can take it out. Thanks Tim.

You still need to watch out for text drops (canvas allows that by default). Enclose all your code in

if obj.FolderItemAvailable then
end if

Or test the converse at the top of your code

if not obj.FolderItemAvailable then return

The point is, you can get to DropObject without a folderitem, and your code will crash in those circumstances.

Thanks Tim. I will do that.