?
Solved

Trying to implement screen capture in VB.NET

Posted on 2008-01-31
3
Medium Priority
?
2,561 Views
Last Modified: 2013-11-26
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

0
Comment
Question by:Frosty555
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
vb_jonas earned 2000 total points
ID: 20794822
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
 
LVL 12

Expert Comment

by:vb_jonas
ID: 20794827
You don't need to add any references.
0
 
LVL 4

Expert Comment

by:J1H1W1
ID: 20795102
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

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The PowerShell Core 6.0 of .NET release is just the beginning. The upcoming PowerShell Core 6.1 would have artificial intelligence and internet of things capabilities. So many things to look forward to in the upcoming release.
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Suggested Courses

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question