You can handle the DragEnter, DragDrop events to do the Drag-and-Drop work. Meanwhile, you should set the PictureBox1.AllowDrop property to true although it’s no browsable in the IntelliSense list, it does exist indeed. While dropping down, get graphics object from the destination image, and draw source image onto the destination image via this graphics object, something like this:
Code Snippet
partial class Form7 : Form
{
public Form7()
{
InitializeComponent();
}
private void Form7_Load(object sender, EventArgs e)
{
this.pictureBox1.AllowDrop = true;
this.pictureBox1.DragEnter += new DragEventHandler(pictureBox1_DragEnter);
this.pictureBox1.DragDrop += new DragEventHandler(pictureBox1_DragDrop);
this.pictureBox2.MouseDown += new MouseEventHandler(pictureBox2_MouseDown);
}
void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.pictureBox2.DoDragDrop(this.pictureBox2.Image, DragDropEffects.All);
}
}
void pictureBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
Bitmap bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
//Get graphics from the Image in the PictureBox1 which is the drop destination.
Graphics g = Graphics.FromImage(this.pictureBox1.Image);
Point location = this.pictureBox1.PointToClient(new Point(e.X, e.Y));
//Draw the draged image onto the destinational image
g.DrawImage(bmp, location);
//Force the PictureBox to redraw himself
this.pictureBox1.Invalidate();
}
}
You can handle the DragEnter, DragDrop events to do the Drag-and-Drop work. Meanwhile, you should set the PictureBox1.AllowDrop property to true although it’s no browsable in the IntelliSense list, it does exist indeed. While dropping down, get graphics object from the destination image, and draw source image onto the destination image via this graphics object, something like this:
Code Snippet
partial class Form7 : Form
{
public Form7()
{
InitializeComponent();
}
private void Form7_Load(object sender, EventArgs e)
{
this.pictureBox1.AllowDrop = true;
this.pictureBox1.DragEnter += new DragEventHandler(pictureBox1_DragEnter);
this.pictureBox1.DragDrop += new DragEventHandler(pictureBox1_DragDrop);
this.pictureBox2.MouseDown += new MouseEventHandler(pictureBox2_MouseDown);
}
void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.pictureBox2.DoDragDrop(this.pictureBox2.Image, DragDropEffects.All);
}
}
void pictureBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
Bitmap bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
//Get graphics from the Image in the PictureBox1 which is the drop destination.
Graphics g = Graphics.FromImage(this.pictureBox1.Image);
Point location = this.pictureBox1.PointToClient(new Point(e.X, e.Y));
//Draw the draged image onto the destinational image
g.DrawImage(bmp, location);
//Force the PictureBox to redraw himself
this.pictureBox1.Invalidate();
}
}
Thanx a lot man, really helped me out here..
|