使用 VB.NET 拍摄屏幕照片

发布于 2024-11-19 04:19:51 字数 962 浏览 2 评论 0原文

目前,我有以下 VB.NET 代码来制作桌面屏幕截图,但它只拍摄活动屏幕的图片:

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

以下代码是我执行上述功能的方式,如果它有任何区别,我不知道我认为确实如此:

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

现在,我需要能够拍摄整个屏幕的照片,而不仅仅是聚焦的窗口。我该怎么做?

预先感谢,

洛根

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暗藏城府 2024-11-26 04:19:51

您应该使用 System.Drawing.Graphics.CopyFromScreen()
参见此处从屏幕复制

只需查询屏幕的完整尺寸作为点传递。
类似于 .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

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
安人多梦 2024-11-26 04:19:51

您的评论说您正在发送alt + printscreen,它只捕获当前活动的窗口。

如果您只发送printscreen,它应该捕获整个桌面。

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.

朮生 2024-11-26 04:19:51

好吧,直接的解决办法是只发送打印屏幕:

SendKeys.Send("{PRTSC}")

但这充其量只是一个蹩脚的黑客攻击。要可靠地截取屏幕截图,您需要使用 P/Invoke 来 GetDC 桌面句柄 (0) 并将 BitBlt 将其内容转换为 Bitmap< /代码>。完成之前,请不要忘记ReleaseDC 桌面的 DC。

或者使用 Graphics.CopyFromScreen< /a>

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

离笑几人歌 2024-11-26 04:19:51

您是否也尝试过不发送 Alt 键?

像这样的东西:

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

Have you tried without sending the Alt key too?

Something like:

SendKeys.Send("{PRTSC}")          '<printscreen> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文