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

LVL 31
Frosty555Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vb_jonasCommented:
Please try this code snippet instead.

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23103105.html?sfQueryTermInfo=1+captur+dual+screen

(use the code in the original questino if you do not need dual screen capture.)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vb_jonasCommented:
You don't need to add any references.
0
J1H1W1Commented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.