Graphics.CopyFromScreen 创建空白图像

发布于 2024-12-05 11:08:58 字数 622 浏览 1 评论 0原文

我正在使用 VB.NET 尝试捕获屏幕的一部分,因此我在几个地方找到了这段代码来捕获整个屏幕:

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

    g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize)

End Using
bitmap.Save("c:\scratch\screenshot.png", System.Drawing.Imaging.ImageFormat.Png)

这可以正常工作,不会抛出错误,并且文件已成功创建,但生成的图像是完全透明的(它尺寸正确)-我缺少什么?

这个问题涉及相同的错误,但解决方案是使用Win32如果可能的话我想避免使用GDI

I am using VB.NET to try and capture a portion of the screen so I found this code in several places to capture the whole screen:

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

    g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize)

End Using
bitmap.Save("c:\scratch\screenshot.png", System.Drawing.Imaging.ImageFormat.Png)

This works with no error throw and the file is created successfully but the resulting image is completely transparent (it is the correct size) - what am I missing?

This question relates to the same error but the solution is to use Win32 GDI which I want to avoid if possible

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

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

发布评论

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

评论(1

中二柚 2024-12-12 11:08:58

由于 CopyFromScreen 方法存在问题,我最终决定采用打印屏幕选项:

'declarations
Private Const VK_SNAPSHOT As Integer = &H2C

<DllImport("user32.dll")>
Public Function keybd_event(ByVal key As Integer, ByVal dummy As Integer, ByVal flags As Integer, ByVal info As IntPtr) As IntPtr
End Function


'Usage
Dim loopCount As Integer = 0

Clipboard.Clear()
keybd_event(VK_SNAPSHOT, 0, 0, IntPtr.Zero)
Application.DoEvents()

Do Until Clipboard.ContainsImage Or (loopCount > 10)
    loopCount += 1
    System.Threading.Thread.Sleep(100)
    Application.DoEvents()
Loop

If Clipboard.ContainsImage Then
    Return Clipboard.GetImage
Else
    Return Nothing
End If

I decided to go along the print screen option in the end due to the issues with the CopyFromScreen method:

'declarations
Private Const VK_SNAPSHOT As Integer = &H2C

<DllImport("user32.dll")>
Public Function keybd_event(ByVal key As Integer, ByVal dummy As Integer, ByVal flags As Integer, ByVal info As IntPtr) As IntPtr
End Function


'Usage
Dim loopCount As Integer = 0

Clipboard.Clear()
keybd_event(VK_SNAPSHOT, 0, 0, IntPtr.Zero)
Application.DoEvents()

Do Until Clipboard.ContainsImage Or (loopCount > 10)
    loopCount += 1
    System.Threading.Thread.Sleep(100)
    Application.DoEvents()
Loop

If Clipboard.ContainsImage Then
    Return Clipboard.GetImage
Else
    Return Nothing
End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文