Screenshot Area (Picturebox)

johnnyj2j

Member
Joined
Mar 4, 2013
Messages
13
Programming Experience
1-3
trying to screen shot the area of my picture box 1 then put it to picture box 2 with this code
VB.NET:
        Dim area As Rectangle = PictureBox1.Bounds
        Dim capture As System.Drawing.Bitmap
        Dim graph As Graphics
        capture = New System.Drawing.Bitmap(Bounds.Width, Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
        graph = Graphics.FromImage(capture)
        graph.CopyFromScreen(area.X, area.Y, 0, 0, area.Size, CopyPixelOperation.SourceCopy)
        PictureBox2.Image = capture
but its not screenshoting the right area (How do I set the position of the top corner of picturebox1 ??
Thanks in advance
 
The Bounds of the PictureBox are relative to its container, i.e. the form or a Panel or whatever its Parent is. You need to specify an area relative to the screen. You can use the RectangleToScreen method of that container to translate the area appropriately, e.g.
Dim area As Rectangle = PictureBox1.Parent.RectangleToScreen(PictureBox1.Bounds)
By the way, while this is not wrong:
graph.CopyFromScreen(area.X, area.Y, 0, 0, area.Size, CopyPixelOperation.SourceCopy)
I would tend to do this:
graph.CopyFromScreen(area.Location, Point.Empty, area.Size, CopyPixelOperation.SourceCopy)
 
You can do that simpler like this:
        Dim bmp As New Bitmap(Me.PictureBox1.Width, Me.PictureBox1.Height)
        Me.PictureBox1.DrawToBitmap(bmp, Me.PictureBox1.ClientRectangle)
        Me.PictureBox2.Image = bmp
 
Back
Top