冰天雪地跪求【屏幕截图】并【将色值存入数组】的代码,非常感谢!!
冰天雪地跪求【屏幕截图】并【将色值存入数组】的代码,非常感谢!!
使用getpixel获取全部色值太慢啦 要好几秒呢~~
我是希望使用这个屏幕色值的数组找图的,希望能有大神帮忙,非常非常感谢!!
[解决办法]
Option Explicit
Private Type BITMAPINFOHEADER '40 bytes
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
End Type
Private Type RGBQUAD
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End Type
Private Type BITMAPINFO
bmiHeader As BITMAPINFOHEADER
bmiColors As RGBQUAD
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject 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 GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function SetDIBits Lib "gdi32" (ByVal hDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Const BI_RGB = 0&
Private Function GetWindowBmp(hwnd As Long, BmpInfo As BITMAPINFO, nBits As Long, Optional mOffset As Long = 0) As Byte()
Dim hDCx As Long
Dim hBmp As Long
Dim r As Long
Dim hMemDC As Long
Dim nWidth As Long, nHeight As Long
Dim RECT As RECT
Dim ImgData() As Byte
Dim mLine As Long
Dim mLineBytes As Long
Call GetWindowRect(hwnd, RECT)
nWidth = RECT.Right - RECT.Left
nHeight = RECT.Bottom - RECT.Top
hDCx = GetDC(hwnd)
hMemDC = CreateCompatibleDC(hDCx)
hBmp = CreateCompatibleBitmap(hDCx, nWidth, nHeight)
r = SelectObject(hMemDC, hBmp)
r = BitBlt(hMemDC, 0, 0, nWidth, nHeight, hDCx, 0, 0, 13369376)
With BmpInfo.bmiHeader
.biSize = Len(BmpInfo.bmiHeader)
.biWidth = nWidth
.biHeight = nHeight
.biPlanes = 1
.biBitCount = nBits
.biCompression = BI_RGB
End With
mLineBytes = (((nWidth * nBits) + &H1F) And &HFFFFFFE0) \ &H8
ReDim ImgData(mLineBytes * nHeight - 1 + mOffset)
mLine = GetDIBits(hDCx, hBmp, 0, nHeight, ImgData(mOffset), BmpInfo, BI_RGB)
GetWindowBmp = ImgData
DeleteDC hMemDC
ReleaseDC hwnd, hDCx
DeleteObject hBmp
End Function
Public Sub Command3_Click()
Dim ImgData() As Byte
Dim mLineBytes As Long
Dim BITMAPINFO As BITMAPINFO
Dim mLineBytesA As Long
Dim mIdx As Long
Dim x As Long, y As Long, C As Long
Dim mLineFromIdx As Long
Dim mBytesPerPix As Long
Dim hDC As Long
hDC = GetDesktopWindow()
ImgData = GetWindowBmp(hDC, BITMAPINFO, 32&) '获取Pic1窗口上显示的位图数据
'到此,获得整个桌面窗口的位图数据到ImgData数组
' With BITMAPINFO.bmiHeader
'
' '计算每行字节数
'
' mLineBytes = .biWidth * .biBitCount / 8
'
' If mLineBytes Mod 4 <> 0 Then
'
' mLineBytesA = ((mLineBytes + 3) \ 4) * 4
'
' Else
'
' mLineBytesA = mLineBytes
'
' End If
'
' mBytesPerPix = .biBitCount / 8 '每像素字节数
'
'
'
' For y = 0 To .biHeight - 1
'
' mIdx = mLineFromIdx
'
' For x = 0 To mLineBytes - 1 Step mBytesPerPix
'
'
'
' C = ImgData(mIdx)
'
' C = (C + ImgData(mIdx + 1) + ImgData(mIdx + 2)) / 3
'
'
'
' ImgData(mIdx) = C
'
' ImgData(mIdx + 1) = C
'
' ImgData(mIdx + 2) = C
'
' mIdx = mIdx + mBytesPerPix
'
' Next
'
' mLineFromIdx = mLineFromIdx + mLineBytesA
'
' Next
'
' End With
'
'
' UserForm1.Show 0
'
'
' SetDIBits UserForm1., UserForm1.Picture.Handle, 0, BITMAPINFO.bmiHeader.biHeight, ImgData(0), BITMAPINFO, BI_RGB
'
''如果Pic2.Image改为Pic1.Image则,将Pic1的彩色改变为黑白
'
' 'Pic2.Refresh
End Sub