Элемент управления WebBrowser имеет функцию DrawToBitmap, но он не работает должным образом. Я не рекомендую это.
Другой вариант - использовать Graphics.CopyFromScreen, надежный, но окно должно быть самым верхним, что раздражает.
Лучшее решение - использовать PrintWindow. Он будет работать на окнах в фоновом режиме, но не на свернутых окнах, а иногда и на окнах, свисающих с края экрана. Этот код взят из Task Switcher , приложения Microsoft, которое заменяет стандартную Windows Alt-Tab:
Public Function CaptureScreen(ByVal R As Rectangle) As Bitmap
Dim b As New Bitmap(R.Width, R.Height)
Dim g As Graphics = Graphics.FromImage(b)
Dim hdc As IntPtr = GetWindowDC(Me.Handle)
If hdc <> IntPtr.Zero Then
Dim hdcMem As IntPtr = CreateCompatibleDC(hdc)
If hdcMem <> IntPtr.Zero Then
Dim hbitmap As IntPtr = CreateCompatibleBitmap(hdc, Me.Width, Me.Height)
If hbitmap <> IntPtr.Zero Then
SelectObject(hdcMem, hbitmap)
PrintWindow(Me.Handle, hdcMem, 0)
BitBlt(g.GetHdc, 0, 0, b.Width, b.Height, hdcMem, R.X, R.Y, TernaryRasterOperations.SRCCOPY)
g.ReleaseHdc()
DeleteObject(hbitmap)
End If
DeleteDC(hdcMem)
End If
ReleaseDC(Me.Handle, hdc)
End If
Return b
End Function
Вам нужно будет определить весь этот Windows API. Это декларации стиля VB, взятые из PInvoke :
''' <summary>
''' Performs a bit-block transfer of the color data corresponding to a
''' rectangle of pixels from the specified source device context into
''' a destination device context.
''' </summary>
''' <param name="hdc">Handle to the destination device context.</param>
''' <param name="nXDest">The leftmost x-coordinate of the destination rectangle (in pixels).</param>
''' <param name="nYDest">The topmost y-coordinate of the destination rectangle (in pixels).</param>
''' <param name="nWidth">The width of the source and destination rectangles (in pixels).</param>
''' <param name="nHeight">The height of the source and the destination rectangles (in pixels).</param>
''' <param name="hdcSrc">Handle to the source device context.</param>
''' <param name="nXSrc">The leftmost x-coordinate of the source rectangle (in pixels).</param>
''' <param name="nYSrc">The topmost y-coordinate of the source rectangle (in pixels).</param>
''' <param name="dwRop">A raster-operation code.</param>
''' <returns>
''' <c>true</c> if the operation succeeded, <c>false</c> otherwise.
''' </returns>
<DllImport("gdi32.dll")> _
Private Shared Function BitBlt(ByVal hdc As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As TernaryRasterOperations) As Boolean
End Function
Private Declare Function PrintWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal hdcBlt As IntPtr, ByVal nFlags As Integer) As Boolean
Private Declare Function CreateCompatibleDC Lib "gdi32.dll" (ByVal hdc As IntPtr) As IntPtr
Private Declare Function DeleteDC Lib "gdi32.dll" (ByVal hdc As IntPtr) As Boolean
Private Declare Function CreateCompatibleBitmap Lib "gdi32.dll" (ByVal hdc As IntPtr, ByVal nWidth As Integer, ByVal nHeight As Integer) As IntPtr
Private Declare Function SelectObject Lib "gdi32.dll" (ByVal hdc As IntPtr, ByVal hgdiobj As IntPtr) As IntPtr
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As IntPtr) As Boolean
Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As IntPtr) As IntPtr
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Integer
Public Enum TernaryRasterOperations
SRCCOPY = &HCC0020 'dest = source
SRCPAINT = &HEE0086 'dest = source OR dest
SRCAND = &H8800C6 'dest = source AND dest
SRCINVERT = &H660046 'dest = source XOR dest
SRCERASE = &H440328 'dest = source AND (NOT dest )
NOTSRCCOPY = &H330008 'dest = (NOT source)
NOTSRCERASE = &H1100A6 'dest = (NOT src) AND (NOT dest)
MERGECOPY = &HC000CA 'dest = (source AND pattern)
MERGEPAINT = &HBB0226 'dest = (NOT source) OR dest
PATCOPY = &HF00021 'dest = pattern
PATPAINT = &HFB0A09 'dest = DPSnoo
PATINVERT = &H5A0049 'dest = pattern XOR dest
DSTINVERT = &H550009 'dest = (NOT dest)
BLACKNESS = &H42 'dest = BLACK
WHITENESS = &HFF0062 'dest = WHITE
End Enum