|
楼主 |
发表于 2007-5-8 00:02:59
|
显示全部楼层
<p>发个GetDIBits... vb环境中312毫秒,,编译运行125毫秒 (p4 2.4GHz 512M xp_sp2 32位 1024*768 )<br/><br/>这样的速度应该没有什么争议吧!!!!!!!?????????????.........<br/><br/>Option Explicit<br/> <br/>Private Type BITMAPINFOHEADER '40 bytes<br/> biSize As Long<br/> biWidth As Long<br/> biHeight As Long<br/> biPlanes As Integer<br/> biBitCount As Integer<br/> biCompression As Long<br/> biSizeImage As Long<br/> biXPelsPerMeter As Long<br/> biYPelsPerMeter As Long<br/> biClrUsed As Long<br/> biClrImportant As Long<br/>End Type<br/><br/>Private Type RGBQUAD<br/> rgbBlue As Byte<br/> rgbGreen As Byte<br/> rgbRed As Byte<br/> rgbReserved As Byte<br/>End Type<br/>Private Type BITMAPINFO<br/> bmiHeader As BITMAPINFOHEADER<br/> bmiColors As RGBQUAD<br/>End Type<br/><br/>Private Type POINT<br/> x As Integer<br/> y As Integer<br/>End Type<br/><br/>Private Const DIB_RGB_COLORS As Long = &H0&<br/>Private Const BI_RGB As Long = &H0&<br/>Private Const OBJ_BITMAP As Long = &H7&<br/>Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, _<br/> ByVal nNumScans As Long, ByVal lpBits As Long, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long<br/>Private Declare Function GetCurrentObject Lib "gdi32" (ByVal hdc As Long, ByVal uObjectType As Long) As Long<br/>Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)<br/>Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long<br/>Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long<br/><br/>Private Function findColor(ByVal hdc As Long, ByVal Width As Integer, ByVal Height As Integer, ByVal find_Color As Long, ByRef retClr() As POINT) As Long<br/> Dim bi As BITMAPINFO<br/> Dim LngCol As Long, hMap As Long, lenBuf As Long, r As Long, s As Long<br/> Dim bmpBuf() As Byte<br/> Dim x As Integer, y As Integer<br/> Erase retClr<br/> find_Color = (&HFF And find_Color) * &H10000 + (&HFF00& And find_Color) + (&HFF0000 And find_Color) / &H10000<br/> With bi.bmiHeader<br/> .biSize = Len(bi.bmiHeader)<br/> .biWidth = Width<br/> .biHeight = Height<br/> .biPlanes = 1<br/> .biBitCount = 24<br/> .biCompression = BI_RGB<br/> End With<br/> lenBuf = CLng(Width) * Height * 3<br/> ReDim bmpBuf(lenBuf - 1)<br/> hMap = GetCurrentObject(hdc, OBJ_BITMAP)<br/> GetDIBits hdc, hMap, 0, bi.bmiHeader.biHeight, VarPtr(bmpBuf(0)), bi, DIB_RGB_COLORS<br/> For y = Height To 1 Step -1<br/> For x = 1 To Width<br/> CopyMemory LngCol, bmpBuf(r), 3<br/> If LngCol = find_Color Then<br/> ReDim Preserve retClr(s)<br/> With retClr(s)<br/> .x = x<br/> .y = y<br/> End With<br/> s = s + 1<br/> End If<br/> r = r + 3<br/> Next<br/> Next<br/> Erase bmpBuf<br/> findColor = s<br/>End Function<br/><br/>Private Sub Command1_Click()<br/> Dim hdc As Long, sint As Single, retClr() As POINT, ret As Long<br/> sint = Timer<br/> hdc = GetDC(0)<br/> ret = findColor(hdc, 1024, 768, &H0, retClr)<br/> ReleaseDC 0, hdc<br/> MsgBox "此颜色点数:" & ret & " 用时: " & (Timer - sint)<br/>End Sub<br/></p><p></p><p></p><p>我找到了一个类似例子;;;</p><p></p><p>但同时发现:<br/>find_Color = (&HFF And find_Color) * &H10000 + (&HFF00& And find_Color) + (&HFF0000 And find_Color) / &H10000<br/><br/>当入参find_Color为4位数如(&HFFFF)会出错。<br/><br/>另外Private Function findColor现在是整个屏幕的,哪位能教我改成某个区域的呢。主要本人对<br/>GetDIBits hdc, hMap, 0, bi.bmiHeader.biHeight, VarPtr(bmpBuf(0)), bi, DIB_RGB_COLORS<br/>此语句不甚了解,麻烦给个思路,先谢谢了。</p> |
|