C# Drag and Drop from one Picture box into Another

无人久伴 提交于 2019-11-28 12:22:30

Drag+drop is hidden on the PictureBox control. Not sure why, it works just fine. The probable guidance here is that it will not be obvious to the user that you could drop an image on the control. You'll have to do something about that, at least set the BackColor property to a non-default value so the user can see it.

Anyhoo, you'll need to implement the MouseDown event on the first picturebox so you can click it and start dragging:

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
        var img = pictureBox1.Image;
        if (img == null) return;
        if (DoDragDrop(img, DragDropEffects.Move) == DragDropEffects.Move) {
            pictureBox1.Image = null;
        }
    }

I assumed you wanted to move the image, tweak if necessary if copying was intended. Then you'll have to implement the DragEnter and DragDrop events on the second picturebox. Since the properties are hidden, you should set them in the form's constructor. Like this:

    public Form1() {
        InitializeComponent();
        pictureBox1.MouseDown += pictureBox1_MouseDown;
        pictureBox2.AllowDrop = true;
        pictureBox2.DragEnter += pictureBox2_DragEnter;
        pictureBox2.DragDrop += pictureBox2_DragDrop;
    }

    void pictureBox2_DragEnter(object sender, DragEventArgs e) {
        if (e.Data.GetDataPresent(DataFormats.Bitmap))
            e.Effect = DragDropEffects.Move;
    }

    void pictureBox2_DragDrop(object sender, DragEventArgs e) {
        var bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
        pictureBox2.Image = bmp;
    }

This does allow you to drag an image from another application into the box. Let's call it a feature. Use a bool flag if you want to disallow this.

You can use mouse enter and leave events to do this easily..For example you have two picture boxes pictureBox1 and pictureBox2... And you want to drag the image from picture box1 and drop it onto picture box2 do somthing like this...

 private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
    {
        if (a == 1)
        {
            pictureBox1.Image = pictureBox2.Image;
            a = 0;
        }
    }

    private void pictureBox1_MouseEnter(object sender, EventArgs e)
    {
        a = 1;
    }

where 'a' is just a lock or key which checks whether the mouse has entered the control on which we want to drop this image on...hope it helped..worked for me

Hans's answer led me to the correct solution. The problem with that answer is that putting DoDragDrop inside MouseDown will prevent MouseClick events from firing.

Here's my solution:

private void PictureBox_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        var pb = (PictureBox)sender;
        if (pb.BackgroundImage != null)
        {
            pb.DoDragDrop(pb, DragDropEffects.Move);
        }
    }
}

private void PictureBox_DragEnter (object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Move;
}

private void PictureBox_DragDrop (object sender, DragEventArgs e)
{
    var target = (PictureBox)sender;
    if (e.Data.GetDataPresent(typeof(PictureBox)))
    {
        var source = (PictureBox)e.Data.GetData(typeof(PictureBox));
        if (source != target)
        {
            // You can swap the images out, replace the target image, etc.
            SwapImages(source, target);
        }
    }
}

Full working example on my GitHub.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!