Copying an image of a picturebox

Shinju

Member
Joined
Jun 24, 2009
Messages
5
Location
Newcastle Upon Tyne, UK
Programming Experience
10+
Hi this one should be (I hope) easy to solve. I'm trying to take a copy of a picture on another picturebox using drawimage, where am I going wrong? (I'm almost certainly doing something silly here.

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim DestRect As Rectangle
Dim SrcRect As Rectangle
DestRect = New Rectangle(20, 20, 50,50)
SrcRect = New Rectangle(30, 30, 50, 50)
e.Graphics.DrawImage(PictureBox2.Image, DestRect, SrcRect, GraphicsUnit.Pixel)
End Sub

Its supposed to take a 50x50 square from picturebox1 at (20,20) and place it on picturebox2 at (30x30) but im getting the error "Value cannot be null. Parameter name: image" and its saying PrictureBox2.image is equal to Nothing.
How come, I am drawing on picturebox2 (with things like drawline, drawrectangle and in the paint event correctly - I hope)

Suggestions?
 
Picturebox will only have a Image if you have set it. When you're drawing in Paint event you're only paiting to screen on request, which is gone next Paint event unless you draw exactly the same then again. What you draw to control can also be drawn to an image by getting a Graphics instance of the image bitmap and calling same code as in Paint handler, or you can copy what is seen on screen with Graphics.CopyFromScreen method.
 

Latest posts

Back
Top