gpt4 book ai didi

vb6 - 从屏幕捕获 Visual Basic RBG

转载 作者:行者123 更新时间:2023-12-04 19:39:55 25 4
gpt4 key购买 nike

我需要在 Visual Basic 中运行的代码来捕获屏幕并将其转换为像素值的 RBG 数组 - 需要非常快。

有什么帮助吗?

最佳答案

此代码将从窗口或整个桌面(虚拟屏幕)捕获屏幕截图并将其绘制到自定义图片框。

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long

Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDC As Long) As Long

Private Const SM_XVIRTUALSCREEN = 76
Private Const SM_YVIRTUALSCREEN = 77
Private Const SM_CYVIRTUALSCREEN = 79
Private Const SM_CXVIRTUALSCREEN = 78

Private Sub GetScreenshot(Optional ByVal hWnd As Long = 0)
Dim hDC As Long

Dim WindowRect As RECT
Dim Left As Long
Dim Top As Long
Dim Width As Long
Dim Height As Long

If hWnd = 0 Then
'Get the DC of the desktop
hDC = GetWindowDC(GetDesktopWindow)

'Get the virtual screen coordinates (this handles multiple monitors too :)
Left = GetSystemMetrics(SM_XVIRTUALSCREEN)
Top = GetSystemMetrics(SM_YVIRTUALSCREEN)
Width = GetSystemMetrics(SM_CXVIRTUALSCREEN)
Height = GetSystemMetrics(SM_CYVIRTUALSCREEN)

Else
'Get the DC of the window we want to capture
hDC = GetWindowDC(hWnd)

'Get the window coordinates
GetWindowRect hWnd, WindowRect
Left = 0
Top = 0
Width = WindowRect.Right - WindowRect.Left
Height = WindowRect.Bottom - WindowRect.Top

End If

'BitBlt into our own DC
BitBlt picScreen.hDC, 0, 0, Width, Height, hDC, Left, Top, vbSrcCopy

'Delete our reference to the windows's DC
ReleaseDC hWnd, hDC
End Function

请注意在捕获桌面时使用 GetSystemMetrics()。这允许它在使用多个显示器而不是 just the primary monitor 时获得完整的虚拟屏幕屏幕尺寸。 .

关于vb6 - 从屏幕捕获 Visual Basic RBG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9677580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com