Click here to Skip to main content
15,885,914 members
Articles / Programming Languages / C#
Article

Drag and Drop Image in C#.NET

Rate me:
Please Sign up or sign in to vote.
3.05/5 (27 votes)
27 Dec 2006CPOL2 min read 194.3K   11.7K   43   12
You can perform drag and drop operations of image data. This article explains how to use the DragEnter,DragLeave, and DoDragDrop events.

Image 1

Introduction

In a Windows application, drag and drop is a common operation performed by users for moving or copying data from one control to another control. In most of the cases, this data may be simple text, file, image, a music file, or it can even be an OLE object. Fortunately, Microsoft .NET technology has given many events and methods by which a programmer can implement drag and drop operation in their applications.

Here I am explaining how to perform drag and drop operation for images. The user can drag an image from one control and drop that image on another control. The control can be a PictureBox or it can be a Panel. I have given a simple application which demonstrates how to drag an image from one panel and drop that image into another panel by using the DragEnter, DragDrop, and MouseDown events and the DragAllow property. You require at least these three events and the property for implementing drag and drop support. Following is the explanation of these three events and the property.

Property

DragAllow

This property is set to true for any control for which we want drag and drop support. This control may be a TextBox, ListBox, or a Panel which holds data.

Events

DragEnter

This event is fired whenever the mouse with a pressed left button will enter into a control’s region. At that time, the control’s DragEnter event is fired. Generally, we require to check the type of data available, which is done as follows.

C#
private void panel_DragEnter(object sender, DragEventArgs e)
{
    // As we are interested in Image data only
    // we will check this as follows
    if (e.Data.GetDataPresent(typeof(Bitmap)))
    {
        e.Effect = DragDropEffects.Copy;
    }
    else
          {
        e.Effect = DragDropEffects.None;
    }
}

MoseDown

Whenever the mouse is pressed on any control, that control’s mouse down event is fired. Generally, in this event, data that we want to drag is given as follows:

C#
private void panel_MouseDown(object sender, MouseEventArgs e)
{
    //we will pass the data that user wants to drag
    //DoDragDrop method is used for holding data
    //DoDragDrop accepts two paramete first paramter 
    //is data(image,file,text etc) and second paramter 
    //specify either user wants to copy the data or move data
            
    Panel source = (Panel)sender;
    DoDragDrop(source.BackgroundImage, 
               DragDropEffects.Copy);
}

DragDrop

This event is fired when the left mouse button is released by the user and the user intends to drop data on the target control. Generally, the target control accepts data here as follows.

C#
private void panel_DragDrop(object sender, DragEventArgs e)
{
    //target control will accept data here 
    Panel destination = (Panel)sender;
    destination.BackgroundImage = (Bitmap)e.Data.GetData(typeof(Bitmap));
}

All the code above is related to Bitmap data only. If you want drag and drop for text data, use the String type.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Microsoft
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDrag Drop Visual Studio 2022 Pin
Member 1598699228-Apr-23 22:49
Member 1598699228-Apr-23 22:49 
Generalthanks Pin
Hooman_Kh23-Sep-16 23:28
Hooman_Kh23-Sep-16 23:28 
QuestionCorrect the property Pin
tqhong6018-Jan-13 15:30
tqhong6018-Jan-13 15:30 
GeneralRe: Correct the property Pin
PIEBALDconsult18-Jan-13 15:38
mvePIEBALDconsult18-Jan-13 15:38 
That's your opinion.
GeneralMy vote of 3 Pin
Ilham Israfilov20-Apr-12 12:43
Ilham Israfilov20-Apr-12 12:43 
QuestionGreat Pin
Member 777135714-Sep-11 1:29
Member 777135714-Sep-11 1:29 
Generalexcellent Pin
jackinelee4-May-11 17:37
jackinelee4-May-11 17:37 
General10 from 10 !!! Pin
raananv19-Apr-11 11:49
raananv19-Apr-11 11:49 
GeneralMy vote of 5 Pin
ShaneMcDonald14-Oct-10 15:35
ShaneMcDonald14-Oct-10 15:35 
GeneralMy vote of 4 Pin
Todd Howard14-Jul-10 10:58
Todd Howard14-Jul-10 10:58 
Question?? Pin
Member 433849325-Jul-09 20:15
Member 433849325-Jul-09 20:15 
GeneralSimple Pin
George_Botros16-Dec-08 5:03
George_Botros16-Dec-08 5:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.