user2015643
user2015643

Reputation: 37

Drag and drop images

With this code I am able to drag and drop an image to the label but now I want to drag that image from the label to another label. How can I do that?

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            Image img = Image.FromFile(@"D:\test\test.png");
            this.btnImage.Image = img;

            Image img1 = Image.FromFile(@"C:\Documents and Settings\SAURABH\Desktop\Green.ico");
            this.btnImage1.Image = img1;
        }

        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            Button btnPic = (Button)sender;
            btnPic.DoDragDrop(btnPic.Image, DragDropEffects.Copy);
        }

        private void button2_MouseDown(object sender, MouseEventArgs e)
        {
            Button btnPic = (Button)sender;
            btnPic.DoDragDrop(btnPic.Image, DragDropEffects.Copy);
        }

        private void button3_MouseDown(object sender, MouseEventArgs e)
        {
            Button btnPic = (Button)sender;
            btnPic.DoDragDrop(btnPic.Image, DragDropEffects.Copy);
        }

        private void label1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Bitmap))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void label10_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Bitmap))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void label10_DragDrop(object sender, DragEventArgs e)
        {
            Label picbox = (Label)sender;
            //PictureBox picbox = (PictureBox)sender;
            Graphics g = picbox.CreateGraphics();
            g.DrawImage((Image)e.Data.GetData(DataFormats.Bitmap), new Point(0, 0));
        }

        private void label1_DragDrop(object sender, DragEventArgs e)
        {
            Label picbox = (Label)sender;
            //PictureBox picbox = (PictureBox)sender;
            Graphics g = picbox.CreateGraphics();
            g.DrawImage((Image)e.Data.GetData(DataFormats.Bitmap), new Point(0, 0));
        }
    }
}

Upvotes: 1

Views: 3834

Answers (1)

Jay Riggs
Jay Riggs

Reputation: 53603

The problem is in your Labels' DragDrop handler. You can directly copy the drag and drop operation's 'payload' with code like this:

private void Label_DragDrop(object sender, DragEventArgs e) {
    if (e.Data.GetDataPresent(typeof(Bitmap))) {
        ((Label)sender).Image = (Image)e.Data.GetData(DataFormats.Bitmap);
    }
}

You might find this MSDN article on drag and drop useful.

I also noticed in your code that the MouseDown, DragEnter and DragDrop event handers are identical for multiple Button and Label controls. You can refactor things a bit by writing each of these handlers once and then registering multiple controls' events to each of these handlers. For example, you can register your Label1 and Label10's DragDrop events to the handler I provided above. Do this in the Designer, or through code:

label1.DragDrop += Label_DragDrop;
label10.DragDrop += Label_DragDrop;

Upvotes: 0

Related Questions