Drag Drop Data pictures to my Form

  • Thread starter Thread starter ravisingh11
  • Start date Start date
R

ravisingh11

Hello all

I have a C# application, with two pictureBox 's I can successfully load
an image in one of the box drag and drop to the other box.

I want to expand this concept so I can drag and drop images from
folders, and other applications. I think I have to use .NET remoting
for this. Any ideas, pointers as to how to do this, would be greatly
appreciated.

Thanks

Ravi Singh
(UCSD)
 
You don't need remoting. You need to handle the DragEnter and DragDrop event
from the control and decide whether the clipboard contains a format of data
you can handle.

In DragEnter, check the clipboard data for formats such as bitmaps or paths
to image files. if the test is positive then allow the drop. When the drop
happens use the data in whatever way your application needs.

For example, this code enables file dropping...

protected override void OnDragEnter(DragEventArgs e)

{

if (e.Data.GetDataPresent(DataFormats.Text))

{

string path =(string) e.Data.GetData("Text");

if(File.Exists(path))

{

e.Effect = DragDropEffects.Copy;

}

else

{

e.Effect=DragDropEffects.None;

}

}

else

{

e.Effect = DragDropEffects.None;

}

base.OnDragEnter(e);

}

This should get you started on thedrop.

protected override void OnDragDrop(DragEventArgs e)
{

if(e.Data.GetDataPresent("Text"))
{
string path=(string)e.Data.GetData("Text");
//load an image from a file??
}
Invalidate();

base.OnDragDrop(e);

}

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Hello,

i have just tried it and this works, but only if the i put the code in the
event handlerss of the form. The Event handlers of the Control (a PictureBox
in my case) are never called when draging a file from the desktop.

Is this a normal behaviour?

thanks,
Pascal
 
Hello Bob

Thanks for replying, as I mentioned in my original post I have the
ability to drag drop pictures within the application. But I wanted to
expand the concept so as to be able to drag drop pictures from
different folders. As Pascal mentioned and I am sure you are aware the
ability of the Drag/Drop event handlers work within the form itself.

So, how do I do this I have been looking into doing it via
IMessageFilter, but little sucess as of now. Anyone have any ideas on
how to do this in C++.

Thanks
Ravi Singh
 
Back
Top