views:

389

answers:

3

Good morning,

I am looking for a good .NET based screen capture utility. It would need be able to capture secure pages (https). It would be preferable it would capture Flash/ActiveX.

Thanks, -Z

A: 

Why does it have to be based in .NET? Why does it have to be limited to browsers?

I use Snagit by Techsmith.

John Saunders
+1  A: 

If are looking to do this from code then you can use the Graphics.CopyFromScreen method as described here: here

Damian Mehers
A: 

The WebBrowser control has a DrawToBitmap function but it doesn't work well. I don't recommend it.

Another option is to use Graphics.CopyFromScreen, reliable but the window must be topmost, which is annoying.

The best solution is to use PrintWindow. It will work on windows in the background, but not on minimized windows and sometimes not on windows that hang off the edge of the screen. This code is taken from Task Switcher, a Microsoft app that replaces the default 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

You'll need to define all that Windows API. These are the VB style declarations, taken from 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
Eyal