Logan
Logan

Reputation: 31

Taking Photo of Screen with VB.NET

Currently I have the following VB.NET code to make a screenshot of my desktop, but it only takes a picture of the active screen:

Public Function SaveScreen(ByVal theFile As String) As Boolean

        Try
            SendKeys.Send("%{PRTSC}")          '<alt + printscreen>
            Application.DoEvents()

            Dim data As IDataObject = Clipboard.GetDataObject()

            If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
                Dim bmp As Bitmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Bitmap)
                bmp.Save(theFile, Imaging.ImageFormat.Png)
            End If
            Clipboard.SetDataObject(0)      'save memory by removing the image from the clipboard
            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function

The following code is how I execute the above function, if it makes any difference, which I don't think it does:

SaveScreen("C:\Lexer_trace\screen.png")

Now, I need to be able to take a picture of the entire screen, not just the focused window. How would I do this?

Thanks in advance,

Logan

Upvotes: 0

Views: 5782

Answers (4)

Jay
Jay

Reputation: 6294

You should be using System.Drawing.Graphics.CopyFromScreen() See Here to copy from a screen

Simply query the full size of the screen to pass in as points. Something similar to what you have with .CopyFromScreen()

Public Sub SaveScreen(filename As String)

    Dim screenSize = SystemInformation.PrimaryMonitorSize
    Dim bitmap = New Bitmap(screenSize.Width, screenSize.Height)
    Dim g = Graphics.FromImage(bitmap)

    g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize)
    g.Flush()
    bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Png)

End Sub

Upvotes: 5

mhttk
mhttk

Reputation: 1696

Have you tried without sending the Alt key too?

Something like:

SendKeys.Send("{PRTSC}")          '<printscreen> 

Upvotes: 0

Blindy
Blindy

Reputation: 67376

Well the immediate fix would be to send only print screen:

SendKeys.Send("{PRTSC}")

But that's a lame hack at best. To take a screenshot of the screen reliably, you need to use P/Invoke to GetDC of the desktop handle (0) and BitBlt its contents into a Bitmap. Don't forget to ReleaseDC the desktop's DC before you're done.

Or use Graphics.CopyFromScreen

Upvotes: 0

ChrisF
ChrisF

Reputation: 137148

Your comment says you are sending alt + printscreen which does just capture the currently active window.

If you just send printscreen it should capture the whole desktop.

Upvotes: 2

Related Questions