2

I searched far and wide looking for the answer to this but nothing seems to give a clear example of how to do it!

I just want to be able to grab a picture from the desktop/explorer and drag and drop it onto a WPF image control or similar.

Can anybody point me in the right direction? I've seen loads of example about dragging and dropping from app to app, or within an app, or from the app to the desktop, but hardly any for the other way round.

I'm guessing some form of clipboard interaction is required.

Nasreddine
  • 36,610
  • 17
  • 75
  • 94
Christian
  • 167
  • 3
  • 13

1 Answers1

5

You need to enable drag and drop on your image control, then its just a matter of opening the file you drop on to it in the event handler.

see the answer from Drag and drop files into WPF (remember to up vote the top answer in that question if it helps :) )

private void ImagePanel_Drop(object sender, DragEventArgs e)
{

  if (e.Data.GetDataPresent(DataFormats.FileDrop))
  {
    // Note that you can have more than one file.
    string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

    // Assuming you have one file that you care about, pass it off to whatever
    // handling code you have defined.
    HandleFileOpen(files[0]);
  }
}
Community
  • 1
  • 1
Eamonn McEvoy
  • 8,876
  • 14
  • 53
  • 83
  • I presume that HandleFileOpen is a method you would define yourself. How would I then set the Image source for the image control from this string? I have only previously done this using resources from the project itself. Many thanks for your help so far :) – Christian Apr 26 '12 at 09:17
  • Yes HandlFileOpen is a method you define yourself, so now that you have the file path you need to open the file and set the image source, see the following article "You can also create image source from a physical file" - http://nahidulkibria.blogspot.co.uk/2009/01/setting-image-source-from-code-wpf.html – Eamonn McEvoy Apr 26 '12 at 20:25