Save form as image (screenshot)

14,481

Replace your TakeScreenShot function with this:

Private Function TakeScreenShot(ByVal Control As Control) As Bitmap
    Dim tmpImg As New Bitmap(Control.Width, Control.Height)
    Using g As Graphics = Graphics.FromImage(tmpImg)
        g.CopyFromScreen(Control.PointToScreen(New Point(0, 0)), New Point(0, 0), New Size(Control.Width, Control.Height))
    End Using
    Return tmpImg
End Function

This should work, however if for some reason it doesn't the problem might be the transparent form on top.

You can call it in excactly the same way.

Good luck :)

Share:
14,481
user3466723
Author by

user3466723

Updated on June 18, 2022

Comments

  • user3466723
    user3466723 almost 2 years

    I have 2 forms.

    • Form 1 contains the content that i need a screenshot of
    • Form 2 contains graphics drawing (this form is always on top but transparent).

    I need to screen shot the first form without making it on top of form 2 as well as without including content from form 2.

    here is some what i am working with, which i am trying to fix.

    Private Function TakeScreenShot(ByVal Control As Control) As Bitmap
        Dim Screenshot As New Bitmap(Control.Width, Control.Height)
        Control.DrawToBitmap(Screenshot, New Rectangle(0, 0, Control.Width, Control.Height))
        Return Screenshot
    End Function
    

    This function is not working because Control.drawtoBitmap is not setting the value of IMG.

    IMG is blank and being returned as a plain white image.

    The calling of this function is this

    TakeScreenShot(form1.webbrowser1).Save("c:\Screenshot.png", 
         System.Drawing.Imaging.ImageFormat.Png)
    

    All help would be appreciated.

  • Fil
    Fil over 8 years
    If you want to capture not only the client portion of yout window, but all the window, just change g.CopyFormScreen instruction this way -> g.CopyFromScreen(Control.Location.X, Control.Location.Y, 0, 0, Control.Size, CopyPixelOperation.SourceCopy)