Windows Develop Bookmark and Share   
 index > Windows Forms General > Drag an image onto another image and join
 

Drag an image onto another image and join

Well basicly the title says it all,
I want to drag a 24Bit PNG (Transparent) onto another PNG image.
Sort of like a watermark..

Anyone who has experience with this ?
Can anyone give me an angle on this, what route i should take on this ?
GDI probably ?

Thanx in advance
Spoofer  Tuesday, August 14, 2007 9:53 PM

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..

Spoofer  Wednesday, August 22, 2007 7:58 PM
Can this be translated to Winform please?
And to integrate it into my application, is it a new class I have to create? (sorry if my questions are a little clunky, i'm new in visual basic).

NAL007  Friday, May 02, 2008 2:47 AM

Hi,

This sample is already a Windows Forms application. I wrote it in C#, I think you can learn the logic, and translate it into VB.NET, it's not that hard, just try it out. :-)

Best Regards

Zhi-xin Ye

Zhi-Xin Ye  Friday, May 02, 2008 4:11 AM
Hi I converted your code to VB.net. I created a form1 with 2 pictureboxes. In the Form1' code view I replaced all its initial code by this code but my PictureBox2 won't move. Are my missing something in the pictureboxes' or form' properties?

Code Snippet

Partial Public Class Form1
Inherits Form


Public Sub New()
InitializeComponent()
End Sub



Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

Me.PictureBox1.AllowDrop = True
AddHandler Me.PictureBox1.DragEnter, AddressOf PictureBox1_DragEnter
AddHandler Me.PictureBox1.DragDrop, AddressOf PictureBox1_DragDrop
AddHandler Me.PictureBox2.MouseDown, AddressOf PictureBox2_MouseDown

End Sub



Private Sub PictureBox2_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)


If e.Button = MouseButtons.Left Then

Me.PictureBox2.DoDragDrop(Me.PictureBox2.Image, DragDropEffects.All)
End If

End Sub



Private Sub PictureBox1_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs)


If e.Data.GetDataPresent(DataFormats.Bitmap) Then



e.Effect = DragDropEffects.Copy
Else




e.Effect = DragDropEffects.None
End If

End Sub



Private Sub PictureBox1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs)


Dim bmp As Bitmap = DirectCast(e.Data.GetData(DataFormats.Bitmap), Bitmap)



'Get graphics from the Image in the PictureBox1 which is the drop destination.

Dim g As Graphics = Graphics.FromImage(Me.PictureBox1.Image)



Dim location As Point = Me.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

Me.PictureBox1.Invalidate()

End Sub

Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click

End Sub
End Class

NAL007  Friday, May 02, 2008 5:36 PM

You can use google to search for other answers

Custom Search

More Threads

• What database to use with Widows apps
• MonthCalendar problem
• IE Crashes when creating new window form having Shockwave control
• Toolbar Icons
• How can i do Multiple selections using either control[Ctrl] key pressing or mouseright button clicking...
• Textbox text gets reversed with / when Right To Left is True
• Dialog box
• Insert records to database in ActiveX control
• Using the .NET 1.1 com interop in .NET 2.0?
• Is there a natural way to make a form increase its width from right to left ?