1

I've been attempting to drag an image from a Picturebox outside my form boundaries just in the same manners that Windows Explorer does, so for instance, dropping it into an external webbrowser to view, Skype to send etc

Switched to VB.NET to find the most basic solution, but no luck there unfortunately. The drag n' drop effect shows outside my form, however, no application accepts or properly responses to the image drop.

Code is ran on the MouseDown event of the Picturebox.

Me.DoDragDrop(PictureBox1.Image, DragDropEffects.All)

Any ideas? C# or VB.NET, both are fine for me.

Thanks

braX
  • 11,506
  • 5
  • 20
  • 33
Karizan
  • 29
  • 6

2 Answers2

0

I found this solution for WPF: Drag and drop to Desktop / Explorer

For winforms works like this:

private void DragDropImage()
{
  var filename = "filename.jpg";
  var path = Path.Combine(Path.GetTempPath(), filename);
  this.pictureBox1.Image.Save(path);
  var paths = new[] { path };
  this.pictureBox1.DoDragDrop(new DataObject(DataFormats.FileDrop, paths), DragDropEffects.Copy);
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
  if (e.Button == MouseButtons.Left)
    this.DragDropImage();
}

This will copy the content of your picturebox to a temp directory. Then makes a dodragdrop. Its tested and works for me. Code is executed in MouseMove and checks if mouse is clicked.

  • Thanks, I have posted an answer comment which cuts the code down to a single line. Highly appreciate your help. – Karizan Jan 29 '18 at 19:53
0

feal's code worked, however, I didn't see the need to waste time/processing power on saving the image so I modified it to the following in case somebody faces the same issue in the future. Code is inserted in the MouseDown event of the control.

VB.NET:

Me.DoDragDrop(New DataObject(DataFormats.FileDrop, {"Image Path"}), DragDropEffects.All)

C#:

this.DoDragDrop(new DataObject(DataFormats.FileDrop, new[] {@"Image Path"}), DragDropEffects.All);

Thanks a lot, feal.

Karizan
  • 29
  • 6