C# - How To Drag And Drop Image In C#
Source code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Drag_Drop_Image : Form
{
public Drag_Drop_Image()
{
InitializeComponent();
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
PictureBox pb = (PictureBox)sender;
pb.Select();
pb.DoDragDrop(pb.Image, DragDropEffects.Copy);
}
private void pictureBox1_DragEnter(object sender, DragEventArgs e)
{
if(e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
PictureBox pb = (PictureBox)sender;
pb.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
}
private void Drag_Drop_Image_Load(object sender, EventArgs e)
{
pictureBox1.AllowDrop = true;
pictureBox2.AllowDrop = true;
pictureBox3.AllowDrop = true;
pictureBox2.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
pictureBox2.DragEnter += new DragEventHandler(pictureBox1_DragEnter);
pictureBox2.DragDrop += new DragEventHandler(pictureBox1_DragDrop);
pictureBox3.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
pictureBox3.DragEnter += new DragEventHandler(pictureBox1_DragEnter);
pictureBox3.DragDrop += new DragEventHandler(pictureBox1_DragDrop);
}
}
}
=> OutPut :
how to drag and drop image in c# |