Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
559 views
in Technique[技术] by (71.8m points)

vb6 - Visual Basic RBG Capture from Screen

I am needing code that will run in visual basic to capture the screen and convert it to a RBG array of pixel values - needs to be quite fast.

Any help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This code will capture a screenshot from a window or the entire desktop (virtual screen) and draw it to a custom picturebox.

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

Note the use of GetSystemMetrics() when capturing the desktop. This allows it to get the full virtual screen screen dimensions when multiple monitors are in use instead of just the primary monitor.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...