[RESOLVED] Take screenshot of control
Hi there,
I have a control on the form where I want a screenshot from. Let's say I have a webcontrol added and navigated to vbforums.com.
How do I create a screenhot of only the webbrowser control? (So no region selector, what-so-ever. Nothing manually)
Thanks in advance.
Re: Take screenshot of control
I am using the stupid sendkey method Frist F11 & Wait & alt Printscreeen & wait & F11 Again
Bewlow have the good project :-
http://www.codeproject.com/Articles/...ntire-Web-Page
Re: Take screenshot of control
No, sorry m8, not what I was looking for. The screenshot should be of the controls visible region. The webpage was just an example. Can also be an imagebox.
But thank you for thinking with me ;)
Re: Take screenshot of control
Had you went to the code project and download WebCapture1b.zip - 162.5 KB - 08.15.2007
Which is the one you needed!
Re: Take screenshot of control
yes I did, but I can't find anything that captures the control. Besides that, I'm having issues loading the project. Please keep in mind that I'm not after the page itself, but the control.
Re: Take screenshot of control
I think i'm getting close:
vb.net Code:
Private Function getBitmap(ByVal pCtrl As Control) As Drawing.Bitmap
Dim myBmp As New Bitmap(pCtrl.Width, pCtrl.Height)
Dim g As Graphics = Graphics.FromImage(myBmp)
Dim p As New Point(pCtrl.Parent.Width - pCtrl.Parent.ClientRectangle.Width, pCtrl.Parent.Height - pCtrl.Parent.ClientRectangle.Height)
g.CopyFromScreen(pCtrl.Parent.Location + pCtrl.Location + p, Point.Empty, myBmp.Size)
Return myBmp
g.Dispose()
End Function
All that calculations to get the correct point is because Form method PointToScreen is not working, at least for me. This codes assume the control is placed directly on the Form (not another container) that's why I use: form.Left (or Top) + Control.Left (or Top) + Form's ControlBox Width (or Height) to get the real Point I need. There is still a problem with this: there is a little offset dissapearing from Left and Top, see it for yourself, i'll see what can I do.
Example on usage:
vb.net Code:
Dim bm As Bitmap = getBitmap(Me.TreeView1) 'Pass any Control as Parameter
Me.BackgroundImage = DirectCast(bm, Drawing.Image)
Re: Take screenshot of control
Well in my case, it works by subtracting 4 pixels for x and y, see this in red here:
Code:
Private Function getBitmap(ByVal pCtrl As Control) As Drawing.Bitmap
Dim myBmp As New Bitmap(pCtrl.Width, pCtrl.Height)
Dim g As Graphics = Graphics.FromImage(myBmp)
Dim pOffset As New Point(pCtrl.Parent.Width - pCtrl.Parent.ClientRectangle.Width - 4, pCtrl.Parent.Height - pCtrl.Parent.ClientRectangle.Height - 4)
g.CopyFromScreen(pCtrl.Parent.Location + pCtrl.Location + pOffset, Point.Empty, myBmp.Size)
Return myBmp
g.Dispose()
End Function
Anyway, the best would be finding out where are those 4 pixels comming from to be able to do something better than just substract them like this, something more generic i mean.
Re: Take screenshot of control
Ola jcis. Thanks will have a look at it and let you know.
like you said, the code doesn't work when the control is in a container which can be an issue, but let's have a closer look-see. ;)
Edit: Well the control in in a container, but it has a fixed position. Can probably work with that.
Re: Take screenshot of control
You can make it work no matter how many containers inside containers you have by doing for example a while loop until control.parent Is Nothing, keep adding left postion for each control in a variable until you reach the Form.
But I don't like that solution, i just think there has a to be an easier way to get a Control's Screen coordinates in .NET, it should be Me.PointToScreen() but (at least for me) it's not working, still there must be a another way to do this.
Re: Take screenshot of control
Looks like i was doing something wrong before, now PointToScreen() is working fine, add this to a module then pass the Form as first parameter and the control as second parameter:
vb.net Code:
Public Function getBitmap(ByVal pForm As Windows.Forms.Form, _
ByVal pCtrl As Control) As Drawing.Bitmap
Dim myBmp As New Bitmap(pCtrl.Width, pCtrl.Height)
Dim g As Graphics = Graphics.FromImage(myBmp)
g.CopyFromScreen(pForm.PointToScreen(pCtrl.Location), Point.Empty, myBmp.Size)
g.Dispose()
Return myBmp
End Function
Re: Take screenshot of control
Or maybe just use the controls parent, seems to work even if the control is inside multiple containers...
Code:
Private Function getBitmap(ByVal pCtrl As Control) As Drawing.Bitmap
Dim myBmp As New Bitmap(pCtrl.Width, pCtrl.Height)
Dim g As Graphics = Graphics.FromImage(myBmp)
Dim pt As Point = pCtrl.Parent.PointToScreen(pCtrl.Location)
g.CopyFromScreen(pt, Point.Empty, myBmp.Size)
g.Dispose()
Return myBmp
End Function
Re: Take screenshot of control
Good idea :thumb:, i didn't know that all container controls have PointToScreen Function.
Re: Take screenshot of control
Re: Take screenshot of control
Uuuh, small question: How do I save the capture as image?
Or do I need to store it to a picturebox first?
This is where I got stuck:
Code:
With sfd
.Title = "Save Screenshot As..."
.DefaultExt = "jpg|*.jpg"
If .ShowDialog = Windows.Forms.DialogResult.OK Then
getBitmap(mycontrol)
.FileName = "...."
End If
End With
EDIT:... Wait.... probably I figured it out.
Re: Take screenshot of control
vb.net Code:
Dim bm As Bitmap = getBitmap(TrackBar1)
bm.Save("C:\myScreenShot.png", Drawing.Imaging.ImageFormat.Png)
..Or whatever other format you want
Re: Take screenshot of control
Thanks m8.+REP for everything you've done. Great help and much appreciated.
Also for Edgemeal.
Re: Take screenshot of control
Quote:
Originally Posted by
jcis
I think i'm getting close:
vb.net Code:
Private Function getBitmap(ByVal pCtrl As Control) As Drawing.Bitmap
Dim myBmp As New Bitmap(pCtrl.Width, pCtrl.Height)
Dim g As Graphics = Graphics.FromImage(myBmp)
Dim p As New Point(pCtrl.Parent.Width - pCtrl.Parent.ClientRectangle.Width, pCtrl.Parent.Height - pCtrl.Parent.ClientRectangle.Height)
g.CopyFromScreen(pCtrl.Parent.Location + pCtrl.Location + p, Point.Empty, myBmp.Size)
Return myBmp
g.Dispose()
End Function
All that calculations to get the correct point is because Form method PointToScreen is not working, at least for me. This codes assume the control is placed directly on the Form (not another container) that's why I use: form.Left (or Top) + Control.Left (or Top) + Form's ControlBox Width (or Height) to get the real Point I need. There is still a problem with this: there is a little offset dissapearing from Left and Top, see it for yourself, i'll see what can I do.
Example on usage:
vb.net Code:
Dim bm As Bitmap = getBitmap(Me.TreeView1) 'Pass any Control as Parameter
Me.BackgroundImage = DirectCast(bm, Drawing.Image)
This is what I'm looking for. Hello from a Newbie :)