Capture Scree

A while ago I created a simple app that captured the desktop screen, and after a search I managed to find the attached code, however in my old application it captured both my screens on my system, however I cant seem to find an example of how to capture the entire desktop area.

Any ideas?
Public Class Form1
    Inherits System.Windows.Forms.Form


    Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As String) As Integer
    Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Integer) As Integer
    Private Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer) As Integer
    Private Declare Function GetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" (ByVal hdc As Integer, ByVal nIndex As Integer) As Integer
    Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Integer, ByVal hObject As Integer) As Integer
    Private Declare Function BitBlt Lib "GDI32" (ByVal srchDC As Integer, ByVal srcX As Integer, ByVal srcY As Integer, ByVal srcW As Integer, ByVal srcH As Integer, ByVal desthDC As Integer, ByVal destX As Integer, ByVal destY As Integer, ByVal op As Integer) As Integer
    Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Integer) As Integer
    Private Declare Function DeleteObject Lib "GDI32" (ByVal hObj As Integer) As Integer
    Const SRCCOPY As Integer = &HCC0020
    Private oBackground As Bitmap

    Private FW, FH As Integer

    Protected Sub CaptureScreen()

        Dim hSDC, hMDC As Integer
        Dim hBMP, hBMPOld As Integer
        Dim r As Integer

        hSDC = CreateDC("DISPLAY", "", "", "")
        hMDC = CreateCompatibleDC(hSDC)

        FW = GetDeviceCaps(hSDC, 8)
        FH = GetDeviceCaps(hSDC, 10)
        hBMP = CreateCompatibleBitmap(hSDC, FW, FH)

        hBMPOld = SelectObject(hMDC, hBMP)
        r = BitBlt(hMDC, 0, 0, FW, FH, hSDC, 0, 0, 13369376)
        hBMP = SelectObject(hMDC, hBMPOld)

        r = DeleteDC(hSDC)
        r = DeleteDC(hMDC)

        oBackground = Image.FromHbitmap(New IntPtr(hBMP))
        DeleteObject(hBMP)

    End Sub
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CaptureScreen()
        PictureBox1.Image = oBackground
    End Sub
End Class

Open in new window

tonelm54Asked:
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.

Mike TomlinsonMiddle School Assistant TeacherCommented:
What version VB.Net are you working with?

You're working too hard, most likely:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim bounds As Rectangle = Screen.GetBounds(Me)
        Dim bmp As New Bitmap(bounds.Width, bounds.Height)
        Using G As Graphics = Graphics.FromImage(bmp)
            G.CopyFromScreen(bounds.Location, New Point(0, 0), bounds.Size)
        End Using
        PictureBox1.Image = bmp
    End Sub

Open in new window

0
tonelm54Author Commented:
Good afternoon 'Idle_Mind' that code captures the screen (a lot easier) but still only my primary monitor :-(

Im using Visual Basic 2010 Express (BETA 2), and running in on Windows 7 Proffesional 32-bit if that helps?

Thank you
0
tonelm54Author Commented:
Ok, Ive found out how to capture the other screens, but is it possible to add onto the original image, and extend it?

Thank you
Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim bounds As Rectangle = Screen.GetBounds(Me)
        Dim bmp As New Bitmap(bounds.Width, bounds.Height)

        Using G As Graphics = Graphics.FromImage(bmp)
            For Each tmpScreen In Screen.AllScreens
                G.CopyFromScreen(tmpScreen.Bounds.Location, New Point(0, 0), tmpScreen.Bounds.Size)
            Next
        End Using

        PictureBox1.Image = bmp
    End Sub
End Class

Open in new window

0
CodeCruiserCommented:
Check if this class achieves that

http://www.vbforums.com/showthread.php?t=385497
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
I haven't worked with multiple monitors so I'm not sure how their coordinate systems work.

You could do something like:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim screens As New List(Of Bitmap)
        For Each tmpScreen As Screen In Screen.AllScreens
            Using bmp As New Bitmap(tmpScreen.Bounds.Width, tmpScreen.Bounds.Height)
                Using G As Graphics = Graphics.FromImage(bmp)
                    G.CopyFromScreen(tmpScreen.Bounds.Location, New Point(0, 0), tmpScreen.Bounds.Size)
                End Using
            End Using
        Next

        Dim TotalWidth, MaxHeight As Integer
        For Each bmp As Bitmap In screens
            TotalWidth = TotalWidth + bmp.Width
            If bmp.Height > MaxHeight Then
                MaxHeight = bmp.Height
            End If
        Next

        Dim CurrentX As Integer
        Dim CompositeBmp As New Bitmap(TotalWidth, MaxHeight)
        Using G As Graphics = Graphics.FromImage(CompositeBmp)
            G.Clear(Color.Black)
            For Each screen As Bitmap In screens
                G.DrawImage(screen, New Point(CurrentX, 0))
                CurrentX = CurrentX + screen.Width
            Next
        End Using
        PictureBox1.Image = CompositeBmp
    End Sub

Open in new window

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
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
Visual Basic.NET

From novice to tech pro — start learning today.