开发者

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开发者_如何学Go 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


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


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.


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


Have you tried without sending the Alt key too?

Something like:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜