Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save form as image (screenshot)

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.

like image 658
user3466723 Avatar asked Dec 08 '22 08:12

user3466723


1 Answers

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 :)

like image 114
Piratica Avatar answered Dec 11 '22 08:12

Piratica