For one of my programs I need some code that will be able to capture a WebBrowser control on TabPage1 and then put that image into a PictureBox on TabPage2.
I've been struggling with finding examples of how to capture hidden windows and what I did find confuses me because I am not very a very experienced programmer.
Here is the code:
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
Private Const WM_PRINT As Integer = &H317
Private Const PRF_CLIENT = &H4& ' Draw the window's client area.
Private Const PRF_CHILDREN = &H10& ' Draw all visible child windows.
Private Const PRF_OWNED = &H20& ' Draw all owned windows.
Private Enum EDrawingOptions As Integer
PRF_CHECKVISIBLE = &H1
PRF_NONCLIENT = &H2
PRF_CLIENT = &H4
PRF_ERASEBKGND = &H8
PRF_CHILDREN = &H10
PRF_OWNED = &H20
End Enum
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim hwnd As Integer = WebBrowser1.Handle.ToInt32()
Dim someBitmap As Bitmap = New Bitmap(PictureBox1.DisplayRectangle.Width, PictureBox1.DisplayRectangle.Height)
Dim someGraphics As Graphics = Graphics.FromImage(someBitmap)
Dim someHDC As IntPtr = someGraphics.GetHdc()
Call SendMessage(hwnd, WM_PRINT, someHDC, EDrawingOptions.PRF_CHILDREN Or EDrawingOptions.PRF_CLIENT Or EDrawingOptions.PRF_NONCLIENT Or EDrawingOptions.PRF_OWNED)
someGraphics.ReleaseHdc(someHDC)
PictureBox1.Image = someBitmap
someGraphics.Dispose()
someGraphics = Nothing
someBitmap = Nothing
End Sub
Sorry I'm not really sure how to post the code in a more assessable way. Okay problem:
It works fine if I go straight to TabPage2 and then click Button1 which will then copy the image of WebBrowser1 and put it as the image of PictureBox1, but if I click once or do anything at all to WebBrowser1 and then click Button1, it will just copy a white screen in place of the old picture. And after interacting with WebBrowser1 in anyway, Button1 will not copy its image again.
I think it might have something to do with me changing the handle of WebBrowser1 in some way or possible invalidating the control. I honestly have no clue.
All help is appreciated.
I've been struggling with finding examples of how to capture hidden windows and what I did find confuses me because I am not very a very experienced programmer.
Here is the code:
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
Private Const WM_PRINT As Integer = &H317
Private Const PRF_CLIENT = &H4& ' Draw the window's client area.
Private Const PRF_CHILDREN = &H10& ' Draw all visible child windows.
Private Const PRF_OWNED = &H20& ' Draw all owned windows.
Private Enum EDrawingOptions As Integer
PRF_CHECKVISIBLE = &H1
PRF_NONCLIENT = &H2
PRF_CLIENT = &H4
PRF_ERASEBKGND = &H8
PRF_CHILDREN = &H10
PRF_OWNED = &H20
End Enum
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim hwnd As Integer = WebBrowser1.Handle.ToInt32()
Dim someBitmap As Bitmap = New Bitmap(PictureBox1.DisplayRectangle.Width, PictureBox1.DisplayRectangle.Height)
Dim someGraphics As Graphics = Graphics.FromImage(someBitmap)
Dim someHDC As IntPtr = someGraphics.GetHdc()
Call SendMessage(hwnd, WM_PRINT, someHDC, EDrawingOptions.PRF_CHILDREN Or EDrawingOptions.PRF_CLIENT Or EDrawingOptions.PRF_NONCLIENT Or EDrawingOptions.PRF_OWNED)
someGraphics.ReleaseHdc(someHDC)
PictureBox1.Image = someBitmap
someGraphics.Dispose()
someGraphics = Nothing
someBitmap = Nothing
End Sub
Sorry I'm not really sure how to post the code in a more assessable way. Okay problem:
It works fine if I go straight to TabPage2 and then click Button1 which will then copy the image of WebBrowser1 and put it as the image of PictureBox1, but if I click once or do anything at all to WebBrowser1 and then click Button1, it will just copy a white screen in place of the old picture. And after interacting with WebBrowser1 in anyway, Button1 will not copy its image again.
I think it might have something to do with me changing the handle of WebBrowser1 in some way or possible invalidating the control. I honestly have no clue.
All help is appreciated.