Results 1 to 17 of 17

Thread: [RESOLVED] Take screenshot of control

  1. #1

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

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


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  2. #2
    Addicted Member
    Join Date
    May 2007
    Posts
    164

    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

  3. #3

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  4. #4
    Addicted Member
    Join Date
    May 2007
    Posts
    164

    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!

  5. #5

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  6. #6
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Take screenshot of control

    I think i'm getting close:
    vb.net Code:
    1. Private Function getBitmap(ByVal pCtrl As Control) As Drawing.Bitmap
    2.         Dim myBmp As New Bitmap(pCtrl.Width, pCtrl.Height)
    3.         Dim g As Graphics = Graphics.FromImage(myBmp)
    4.  
    5.         Dim p As New Point(pCtrl.Parent.Width - pCtrl.Parent.ClientRectangle.Width, pCtrl.Parent.Height - pCtrl.Parent.ClientRectangle.Height)
    6.         g.CopyFromScreen(pCtrl.Parent.Location + pCtrl.Location + p, Point.Empty, myBmp.Size)
    7.  
    8.         Return myBmp
    9.         g.Dispose()
    10.     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:
    1. Dim bm As Bitmap = getBitmap(Me.TreeView1) 'Pass any Control as Parameter
    2. Me.BackgroundImage = DirectCast(bm, Drawing.Image)
    Last edited by jcis; May 24th, 2012 at 11:00 AM.

  7. #7
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    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.

  8. #8

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  9. #9
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    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.

  10. #10
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    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:
    1. Public Function getBitmap(ByVal pForm As Windows.Forms.Form, _
    2.                               ByVal pCtrl As Control) As Drawing.Bitmap
    3.         Dim myBmp As New Bitmap(pCtrl.Width, pCtrl.Height)
    4.         Dim g As Graphics = Graphics.FromImage(myBmp)
    5.  
    6.         g.CopyFromScreen(pForm.PointToScreen(pCtrl.Location), Point.Empty, myBmp.Size)
    7.         g.Dispose()
    8.         Return myBmp
    9.     End Function

  11. #11
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    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

  12. #12
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    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.

  13. #13

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Take screenshot of control

    Will try it right away.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  14. #14

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  15. #15
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Take screenshot of control

    vb.net Code:
    1. Dim bm As Bitmap = getBitmap(TrackBar1)
    2. bm.Save("C:\myScreenShot.png", Drawing.Imaging.ImageFormat.Png)
    ..Or whatever other format you want

  16. #16

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Take screenshot of control

    Thanks m8.+REP for everything you've done. Great help and much appreciated.
    Also for Edgemeal.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  17. #17
    Registered User Akosigrasya's Avatar
    Join Date
    Jul 2017
    Posts
    1

    Re: Take screenshot of control

    Quote Originally Posted by jcis View Post
    I think i'm getting close:
    vb.net Code:
    1. Private Function getBitmap(ByVal pCtrl As Control) As Drawing.Bitmap
    2.         Dim myBmp As New Bitmap(pCtrl.Width, pCtrl.Height)
    3.         Dim g As Graphics = Graphics.FromImage(myBmp)
    4.  
    5.         Dim p As New Point(pCtrl.Parent.Width - pCtrl.Parent.ClientRectangle.Width, pCtrl.Parent.Height - pCtrl.Parent.ClientRectangle.Height)
    6.         g.CopyFromScreen(pCtrl.Parent.Location + pCtrl.Location + p, Point.Empty, myBmp.Size)
    7.  
    8.         Return myBmp
    9.         g.Dispose()
    10.     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:
    1. Dim bm As Bitmap = getBitmap(Me.TreeView1) 'Pass any Control as Parameter
    2. 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
  •  



Click Here to Expand Forum to Full Width