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.
[code]
//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.
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?
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
txtFile.Text=Tool
ft.name=txtFile.Text
//Copies file to target location
f.CopyFileTo(SpecialFolder.DeskTop)
f.CopyFileTo(getfolderitem("").child(“Inventory”).child(ft.name))[/code]
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+
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.