|
-
May 24th, 2012, 08:27 AM
#1
Thread Starter
PowerPoster
[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.
-
May 24th, 2012, 09:20 AM
#2
Addicted Member
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
-
May 24th, 2012, 09:23 AM
#3
Thread Starter
PowerPoster
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
Last edited by Radjesh Klauke; May 24th, 2012 at 09:26 AM.
-
May 24th, 2012, 09:38 AM
#4
Addicted Member
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!
-
May 24th, 2012, 10:04 AM
#5
Thread Starter
PowerPoster
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.
-
May 24th, 2012, 10:56 AM
#6
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)
Last edited by jcis; May 24th, 2012 at 11:00 AM.
-
May 24th, 2012, 11:20 AM
#7
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.
-
May 24th, 2012, 12:11 PM
#8
Thread Starter
PowerPoster
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.
-
May 24th, 2012, 12:26 PM
#9
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.
-
May 24th, 2012, 12:53 PM
#10
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
-
May 24th, 2012, 01:01 PM
#11
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
-
May 24th, 2012, 01:03 PM
#12
Re: Take screenshot of control
Good idea , i didn't know that all container controls have PointToScreen Function.
Last edited by jcis; May 24th, 2012 at 01:07 PM.
-
May 24th, 2012, 01:03 PM
#13
Thread Starter
PowerPoster
Re: Take screenshot of control
-
May 24th, 2012, 01:22 PM
#14
Thread Starter
PowerPoster
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.
Last edited by Radjesh Klauke; May 24th, 2012 at 01:26 PM.
-
May 24th, 2012, 01:26 PM
#15
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
-
May 24th, 2012, 01:27 PM
#16
Thread Starter
PowerPoster
Re: Take screenshot of control
Thanks m8.+REP for everything you've done. Great help and much appreciated.
Also for Edgemeal.
-
Jul 21st, 2017, 01:27 AM
#17
Registered User
Re: Take screenshot of control
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|