Save form as image (screenshot)

你离开我真会死。 提交于 2019-12-24 01:52:53

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/22676601/save-form-as-image-screenshot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!