1

I have a Application that can Drag n Drop SVG Files. This is a Tool for Applications like Adobe XD (which support SVG Files). To perform a basic drag n drop operation is really simple.

Dim dataObj As New DataObject(DataFormats.FileDrop, New String() {_svgPath})
PictureBox.DoDragDrop(dataObj, DragDropEffects.Copy)

And it works! If I want to drag n drop a file to the explorer then everything works fine. Also if I Drag n Drop a .png file to paint or a string to the editor: It works.

Now the problem: When it comes to other third party application then it will get a bit confusing. You want to Drag n Drop a SVG file from your app to Adobe XD? Nope, this will not work! You want to Drag n Drop a SVG File from a Folder to Adobe XD? This will work without problems.

This confusing situation not only appears to Adobe XD. They are tons of other programs out there where the Drag n Drop Operation works when using the explorer but your "own created" drag n drop operation failed.

I already tried out a lot. Tested it with different DataFormats, different DragDropEffects… nothing is working. It is like I missed something. I already searched on GitHub for different DoDragDrop() function, searched in the .NET Docs just to see if something is missing.

I found this in the reference source of System.Windows.Forms.Control about the DoDragDrop() function:

Begins a drag operation. The allowedEffects determine which drag operations can occur. If the drag operation needs to interop with applications in another process, data should either be a base managed class (String, Bitmap, or Metafile) or some Object that implements System.Runtime.Serialization.ISerializable. data can also be any Object that implements System.Windows.Forms.IDataObject.

But this info was also not really helpful.

How does the windows explorer create its DragDrop operation and how can I make such a DragDrop operation in .Net (irrelevant if its in vb.net or c#)? Do I need to do more with the DataObject? transform it? change it?

8
  • 1
    Since your DataObject is an array of strings, you shouldn't need to use a custom ISerializable object. Try boosting your DragDropEffect: pictureBox1.DoDragDrop(dataObj, (DragDropEffects.All | DragDropEffects.Link));. Mind that _svgPath must be a Fully Qualified Path. This works with PhotoShop (tested).
    – Jimi
    Aug 1, 2018 at 13:30
  • @Jimi thank you for the tip but I already tried this. It is also interesting to see that if you use the copyboard then you will have the same behavior. If you copy the file to the clipboard via the explorer then you can paste it without problems into Adobe XD in my case. Maybe the DataObject is the problem. I could try to add the data as a Byte array to a MemoryStream and pass the MemoryStream to the SetData method. Aug 1, 2018 at 14:25
  • Yes, a DragDrop DataObject can be a MemoryStream. But it shouldn't be needed here. Of course, if that program accepts this input (array of strings -> File Paths) from a DragDrop result. Have you tried with PhotoShop or the WebBrowser you're using? I tested a multi file drop on both (the WB is FireFox) and both accept the drop and show the Bitmaps. Test it yourself. If it's not working, there's something else to verify in your code. If it works, than Adobe XD is a little picky and you'll have to find out why (I don't have it).
    – Jimi
    Aug 1, 2018 at 14:34
  • It just came to mind that I posted a small project that tests Drag&Drop results from a WebBrowser control (or whatever else). You can download it from here. Maybe it can help to determine what happens when you drop your DataObject content.
    – Jimi
    Aug 1, 2018 at 14:43
  • @Jimi Thanks for the info. Yeah it its a big different between the windows explorer and my own app. I uploaded a screenshot on Twitter here - Alright I guess I will try out what I can do with the MemoryStream Aug 1, 2018 at 20:26

2 Answers 2

2

These are the bare-bones requirements for a Drag&Drop operation that involves a DataFormats.FileDrop data type.
Includes a Thumbnail image of the source file, if the source file type is supported by the Windows GDI related methods (BitBlt, mostly).

When the Drag&Drop operation is started - dragging the Mouse over a PictureBox - the original .svg file path is added to a specialized StringCollection class.
The StringCollection path is then included in the DataObject collection passed to the DoDragDrop method using the DataObject.SetFileDropList() method, along with an extended selection of DragDropEffects (set as required).

The SVGImagePath field used here refers to the original .svg file path.

Win GDI Image source (Jpeg) tested with PhotoShop, FireFox, Windows Paint, Visual Studio Image Editor.
.svg Image tested with FireFox and Edge.

.Net Version: 4.7.1
Visual Studio version: 15.7.5

Imports System.Collections.Specialized

Private SVGImagePath As String = "[Source SVG File]"

Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
    If sender IsNot Nothing AndAlso (e.Button = MouseButtons.Left) Then

        Dim dataObject As New DataObject()
        Dim FilePathsItems As New StringCollection()
        FilePathsItems.Add(SVGImagePath)

        Dim thumbCallback As Image.GetThumbnailImageAbort =
            Function()
                Return False
            End Function
        dataObject.SetImage(New Bitmap(PictureBox1.Image.GetThumbnailImage(64, 64, thumbCallback, IntPtr.Zero)))
        dataObject.SetFileDropList(FilePathsItems)
        PictureBox1.DoDragDrop(dataObject, (DragDropEffects.Copy Or DragDropEffects.Link))
    End If
End Sub

C# original code:

using System.Collections.Specialized;

private string SVGImagePath = string.Empty;

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        //Image.GetThumbnailImageAbort thumbCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
        //FilePathsItems.Add(((FileInfo)listBox1.SelectedItem).FullName);
        DataObject dataObject = new DataObject();
        StringCollection FilePathsItems = new StringCollection();
        FilePathsItems.Add(SVGImagePath);

        Image.GetThumbnailImageAbort thumbCallback = ()=> { return false; };
        dataObject.SetImage(new Bitmap(pictureBox1.Image.GetThumbnailImage(64, 64, thumbCallback, IntPtr.Zero)));
        dataObject.SetFileDropList(FilePathsItems);
        pictureBox1.DoDragDrop(dataObject, (DragDropEffects.Copy | DragDropEffects.Link));
    }
}
7
  • Wow thank you for your effort but this don't solve my problem. I also checked with the "Drag Drop Result Tool" from you if something changed. The output is still the same. I already try something else. I try to preserve the data format, add the data as a Byte array to a MemoryStream and pass the MemoryStream to the SetData method. Maybe then I get the MemoryStream Meta Infos which is needed for the D&D operation with other programs like Adobe XD (which seems to check this info and ignore the file if it is only the simple Bitmap FileDrop). Aug 2, 2018 at 20:24
  • Well, it looks strange that just that program does not accept the Drop. Have you tested it with at least one WebBrowser (that accepts .svg files)? (Btw, what image are you showing in that PictureBox?). Also, a D&D related to files, usually does not transfer the actual data. What if you selected an dropped some Gigs of files? You transfer just the references.
    – Jimi
    Aug 2, 2018 at 20:34
  • the old function mentioned in my question already works with a WebBrowser. The PictureBox shows the image which will be dragged. what do you mean with "gigs of files"? I guess some application listen for meta infos like the "FileGroupDescriptor", "DragContext", "FileContents" or similiar. I try to add this info somehow in the Object for the D&D operation. Maybe I need to play with a COM interface. Aug 2, 2018 at 20:50
  • gigs = Gigabytes. If you select from Explorer files for 20Gb, how do you put them in memory? You don't. Explorer (and every on else) just adds the references (paths) to the DataObject. The PictureBox image: PictureBoxes don't support the .svg format (GDI doesn't). What are you showing there? A substitute image?
    – Jimi
    Aug 2, 2018 at 20:56
  • It shows a .png of the .svg. I just want to take advantage of the MemoryStream to get the meta info. Maybe it works. But I have not found a solution yet. The Windows Explorer packs a lot more info in the drag n drop process then a normal DataObject can handle. You can see it in the screenshot I've already shown you. here is it again. Aug 2, 2018 at 21:02
0

Jimi's answer is correct. My problem in Jimi's code is the generation of the Thumbnail Image. Your target application could block the D&D operation because of it. If you wait some seconds (maybe GetThumbnailImage take its time) then the target application will allow the file. I leave my personal code as answer.

Dim dataObj As New DataObject
Dim filePaths = New List(Of String) From {New System.IO.FileInfo(_svgPath).FullName}
Dim sc = New System.Collections.Specialized.StringCollection()
sc.AddRange(filePaths.ToArray())
dataObj.SetFileDropList(sc)
PictureBox.DoDragDrop(dataObj, DragDropEffects.Copy)

And again: Special thanks to @Jimi for his effort <3

6
  • This is exactly what I wrote in the description (...) the original .svg file path is added to a specialized StringCollection class. (...). And in the code: Dim FilePathsItems As New StringCollection() FilePathsItems.Add(SVGImagePath). This is how you add file path to a DataObject collection. You didn't read what I posted, right?
    – Jimi
    Aug 3, 2018 at 7:54
  • @Jimi Oh sorry I already forgot it. I still try to find out why it works now. Maybe it works because I added the file to the StringCollection like this: New System.IO.FileInfo(_svgPath).FullName - I will edit my answer. Aug 3, 2018 at 8:11
  • You always add a Fully Qualified Path, never a relative path or a path built with somepart & someotherpart & filename. Use Path.Combine() in that case. FileInfo().FullName is just a string reporting the Fully Qualified File Path (c:\Something\Somethingelse\FileName). And that's what you always have to do.
    – Jimi
    Aug 3, 2018 at 8:17
  • @Jimi Ok then this is also not the real solution because I only work with the full file path (otherwise I would have some other problems haha). I will take your code and see what I need to modify so that it works. Aug 3, 2018 at 8:22
  • @Jimi checked it again and accepted your answer. I also updated my answer with the reason why your code was not working for me. Aug 3, 2018 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.