Link to home
Start Free TrialLog in
Avatar of Frosty555
Frosty555Flag for Canada

asked on

Trying to implement screen capture in VB.NET

I'm fairly new to VB.NET. I'm trying to implement this example in VB.NET, to screen capture the screen and save it as a jpeg:
http://www.vbdotnetheaven.com/Uploadfile/mgold/ScreenCaptureUtilityVB11162005063518AM/ScreenCaptureUtilityVB.aspx

I have done this once before successfully in VB6, a while back. So I'm familiar with the concept of DCs and bitblting and the GDI library... from a pre-.net perspective.

I figured out that I needed to add System.Drawing and System.Drawing.Imaging to my references in my VB.NET project, but then it complained that it didn't know what CreateDC() or BitBlt() was. Now I know these are API functions... so I looked up their definitions and I found:

   Declare Function CreateDC Lib "gdi32.dll" (ByVal lpszDriver As String, ByVal lpszDevice As String, ByVal lpszOutput As String, ByVal lpInitData As IntPtr) As IntPtr

    Declare Function BitBlt Lib "gdi32.dll" (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

Which was fairly reasonable, but when I ran my program, I got the error:

 "Unable to find an entry point named 'CreateDC' in DLL 'gdi32.dll'."

The example link I provided above never mentioned anything about having to add references or declares to the project... perhaps I'm just doing it wrong. Can anyone help me get this working?

I've attached all my relevant code in the snippet
Enum TernaryRasterOperations As UInteger
        SRCCOPY = 13369376     'dest = source
        SRCPAINT = 15597702    'dest = source OR dest
        SRCAND = 8913094       'dest = source AND dest
        SRCINVERT = 6684742    'dest = source XOR dest
        SRCERASE = 4457256     'dest = source AND (NOT dest )
        NOTSRCCOPY = 3342344   'dest = (NOT source)
        NOTSRCERASE = 1114278  'dest = (NOT src) AND (NOT dest) 
        MERGECOPY = 12583114   'dest = (source AND pattern)
        MERGEPAINT = 12255782  'dest = (NOT source) OR dest
        PATCOPY = 15728673     'dest = pattern
        PATPAINT = 16452105    'dest = DPSnoo
        PATINVERT = 5898313    'dest = pattern XOR dest
        DSTINVERT = 5570569    'dest = (NOT dest)
        BLACKNESS = 66         'dest = BLACK
        WHITENESS = 16711778   'dest = WHITE
    End Enum
 
    Declare Function CreateDC Lib "gdi32.dll" (ByVal lpszDriver As String, ByVal lpszDevice As String, ByVal lpszOutput As String, ByVal lpInitData As IntPtr) As IntPtr
    Declare Function BitBlt Lib "gdi32.dll" (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
 
 
    Public Sub PerformCapture()
        Dim MyImage As Bitmap
 
        'use the GDI call and create a DC to the whole display
        Dim dc1 As IntPtr = CreateDC("DISPLAY", Nothing, Nothing, CType(Nothing, IntPtr))
 
        'create a Graphics object for the screen dc
        Dim g1 As Graphics = Graphics.FromHdc(dc1)
 
        ' create a compatible bitmap the size of the entire screen
        MyImage = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, g1)
 
        ' use the bitmap to create another Graphics surface so we can BitBlt into the bitmap
        Dim g2 As Graphics = Graphics.FromImage(MyImage)
 
        ' Now go retrace our steps and get the device contexts for both the bitmap and the screen
        ' Note: Apparently you have to do this, and can't go directly from the aquired dc or exceptions are thrown
        ' When you try to release the dcs
        dc1 = g1.GetHdc()
        Dim dc2 As IntPtr = g2.GetHdc()
 
        ' BitBlt the screen into the Bitmap
        BitBlt(dc2, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, dc1, 0, 0, 13369376)
 
        ' Remember to release the dc's, otherwise problems down the road
        g1.ReleaseHdc(dc1)
        g2.ReleaseHdc(dc2)
 
        ' Save the image to JPEG file
        MyImage.Save("c:\Captured.jpg", ImageFormat.Jpeg)
 
        MessageBox.Show("Finished Saving Image")
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of vb_jonas
vb_jonas
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You don't need to add any references.
Avatar of J1H1W1
J1H1W1

In .Net use CopyFromScreen to implement screen capture.
    Dim Bmp As New Bitmap _
    (My.Computer.Screen.Bounds.Width, _
     My.Computer.Screen.Bounds.Height)
    Dim G As Graphics = Graphics.FromImage(Bmp)
    G.CopyFromScreen _
    (New Point(0, 0), _
     New Point(0, 0), _
     My.Computer.Screen.Bounds.Size, _
     CopyPixelOperation.SourceCopy)
    G.Dispose()
    Bmp.Save("C:\Capture.png")
    'jpg is not a good format for line art.

Open in new window