Image Drop : Drag Drop « GUI Windows Form « C# / C Sharp






Image Drop

 

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

class ImageDrop : Form {
    bool bIsTarget;
    Image image;
    public static void Main() {
        Application.Run(new ImageDrop());
    }
    public ImageDrop() {
        AllowDrop = true;
    }
    protected override void OnDragOver(DragEventArgs dea) {
        if (dea.Data.GetDataPresent(DataFormats.FileDrop) || dea.Data.GetDataPresent(typeof(Metafile)) || dea.Data.GetDataPresent(typeof(Bitmap))) {
            if ((dea.AllowedEffect & DragDropEffects.Move) != 0)
                dea.Effect = DragDropEffects.Move;
            if (((dea.AllowedEffect & DragDropEffects.Copy) != 0) && ((dea.KeyState & 0x08) != 0))    // Ctrl key
                dea.Effect = DragDropEffects.Copy;
        }
    }
    protected override void OnDragDrop(DragEventArgs dea) {
        if (dea.Data.GetDataPresent(DataFormats.FileDrop)) {
            string[] astr = (string[])dea.Data.GetData(DataFormats.FileDrop);
            image = Image.FromFile(astr[0]);
            Invalidate();
        } else {
            if (dea.Data.GetDataPresent(typeof(Metafile)))
                image = (Image)dea.Data.GetData(typeof(Metafile));
            else if (dea.Data.GetDataPresent(typeof(Bitmap)))
                image = (Image)dea.Data.GetData(typeof(Bitmap));
            bIsTarget = true;
            Invalidate();
        }
    }
    protected override void OnMouseDown(MouseEventArgs mea) {
        if (image != null) {
            bIsTarget = false;
            DragDropEffects dde = DoDragDrop(image,DragDropEffects.Copy | DragDropEffects.Move);
            if (dde == DragDropEffects.Move && !bIsTarget)
                image = null;
        }
    }
}

 








Related examples in the same category

1.Drag and drop inside a containerDrag and drop inside a container
2.Drag and drop image to another windowDrag and drop image to another window
3.Drag and drop the PictureBoxDrag and drop the PictureBox
4.Fake Drag And DropFake Drag And Drop