[求助]请问怎么获取屏幕区域所有像素??
<p>请大马帮忙</p><p>怎么获取屏幕区域所有像素??</p><p>然后保存为 2种信息</p><p>1.每个像素点的颜色值<br/>2.每个像素点的坐标</p><p></p><p>//我找了个大概的代码...但是看不懂...不知道是不是我要的代码...</p><p>///</p>'过程一: 获得一个在PICTURE控件中打开的图像的所有像素<br/>Public Sub DibGet(ByVal IdSource As Long, XBegin As Long, ByVal YBegin As Long, ByVal XEnd As Long, ByVal YEnd As Long)<br/>Dim iBitmap As Long<br/>Dim iDC As Long<br/>Dim I As Long<br/>Dim W As Long<br/>Dim H As Long<br/> On Error GoTo ErrLine<br/> InPutWid = XEnd - XBegin<br/> InPutHei = YEnd - YBegin<br/> W = InPutWid + 1<br/> H = InPutHei + 1<br/> I = (Bits \ 8) - 1<br/> ReDim ColVal(I, InPutWid, InPutHei)<br/> With bi24BitInfo.bmiHeader<br/> .biBitCount = Bits<br/> .biCompression = 0&<br/> .biPlanes = 1<br/> .biSize = Len(bi24BitInfo.bmiHeader)<br/> .biWidth = W<br/> .biHeight = H<br/> End With<br/> iBitmap = GetCurrentObject(IdSource, 7&)<br/> GetDIBits IdSource, iBitmap, 0&, H, ColVal(0, 0, 0), bi24BitInfo, 0&<br/> DeleteObject iBitmap<br/> Exit Sub<br/>ErrLine:<p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p> <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> <p>参考其他论坛地址</p><p></p><p><a href="http://www.vbgood.com/viewthread.php?tid=38272&extra=&page=1">http://www.vbgood.com/viewthread.php?tid=38272&extra=&page=1</a></p> <p>GetDIBits hdc, hMap, 0, bi.bmiHeader.biHeight, VarPtr(bmpBuf(0)), bi, DIB_RGB_COLORS</p><p>这一句貌似是把某DC的内容读到数组中.</p><p>记得以前看过一个读取BMP象素的代码,就是这样写的.</p><p>对数组作循环,一般来说还是蛮快的.</p><p>这个方法也很不错.</p> <p>帮我整理一个出来好吗....我感觉很乱,,看不懂..</p><p></p><p>改成在某个区域查找某个颜色,,返回坐标的 谢谢 <br/> <br/></p> <p>自动加血加蓝?</p><p>呵呵</p> <p>findColor这个函数就是这个功能呀.</p><p>findColor(hdc, 1280, 800, RGB(11, 198, 17), retClr)</p><p>第一个参数是你要找的目标的DC,得自己想办法得到;</p><p>第二与第三个参数是坐标最大范围;</p><p>第四个参数是要找的颜色的值</p><p>最后一个参数是用于返回所找到的点的坐标.</p><p>函数返回值是此颜色的点的数量.</p> <p>Find_Color = (&HFF And Find_Color) * &H10000 + (&HFF00& And Find_Color) + (&HFF0000 And Find_Color) / &H10000</p><p>这句啥意思???</p><p></p><p></p><p> CopyMemory LngCol, bmpBuf(r), 3</p><p>这句啥意思??? </p><p></p><p>lenBuf = CLng(Width) * Height * 3</p><p>这句啥意思??? </p><p></p><p>ReDim Preserve retClr(s)</p><p>这句啥意思???</p><p></p><p></p><p> Erase bmpBuf</p><p>这句啥意思???</p> <p>Find_Color = (&amp;HFF And Find_Color) * &amp;H10000 + (&amp;HFF00&amp; And Find_Color) + (&amp;HFF0000 And Find_Color) / &amp;H10000</p><p> 貌似某种转换...?</p><p>CopyMemory LngCol, bmpBuf(r), 3</p><p> 指针操作,将3个字节的BYTE复制到一个LONG的内存里去</p><p>lenBuf = CLng(Width) * Height * 3</p><p> 这个忘了.....与DIB位图格式有关的......好象是每个象素使用0-255的三个值来代表它的颜色.....?</p><p>ReDim Preserve retClr(s)</p><p> 在保存数组内容的前提下,对数组扩维</p><p>Erase bmpBuf</p><p> 释放数组所占内存</p> <p>这样吧..再说个用途....</p><p></p><p>就是想用来做 Photoshop的魔术棒 功能</p> <p>感谢...不过不是我想要的效果...</p><p></p><p></p> <p><br/>'iLeft,iTop,iRight,iBottom,szColor<br/>Private Function FindColor(ByVal iLeft As Long, ByVal iTop As Long, ByVal iRight As Long, ByVal iBottom As Long, _<br/> ByVal szColor As Long, ByRef FoundX As Long, ByRef FoundY As Long)</p><p><br/>Dim HDC As Long<br/>Dim IntX As Long, IntY As Long<br/>Dim r As Long, s As Long</p><p>Dim Bi As BITMAPINFO</p><p>Dim LngCol As Long, Hmap As Long, LENbuf As Long<br/>Dim BMPbuf() As Byte</p><p><br/> 'szColor = (&HFF And szColor) * &H10000 + (&HFF00& And szColor) + (&HFF0000 And szColor) / &H10000</p><p><br/> With Bi.bmiHeader<br/> .biSize = Len(Bi.bmiHeader)<br/> .biWidth = iRight<br/> .biHeight = iBottom<br/> .biPlanes = 1<br/> .biBitCount = 24<br/> .biCompression = BI_RGB<br/> End With</p><p><br/> LENbuf = CLng(iRight) * iBottom * 3<br/> ReDim BMPbuf(LENbuf - 1)</p><p><br/> HDC = GetDC(0)<br/> Hmap = GetCurrentObject(HDC, OBJ_BITMAP)<br/> GetDIBits HDC, Hmap, 0, Bi.bmiHeader.biHeight, VarPtr(BMPbuf(0)), Bi, DIB_RGB_COLORS<br/> ReleaseDC 0, HDC</p><p><br/> For IntY = iTop To iBottom<br/> For IntX = iLeft To iRight<br/> CopyMemory LngCol, BMPbuf(r), 3<br/> If LngCol = szColor Then<br/> FoundX = IntX<br/> FoundY = IntY<br/> Erase BMPbuf '释放数组空间<br/> Exit Function<br/> End If<br/> r = r + 3<br/> Next<br/> Next<br/> FoundX = -1<br/> FoundY = -1</p><p><br/>End Function</p><p></p><p>Private Sub Command1_Click()<br/>Dim IntX As Long, IntY As Long<br/>Dim sint As Long<br/> sint = Timer<br/> '左,上, 右, 下, 颜色, x坐标, y坐标<br/> Call FindColor(1, 1, 1023, 767, 255, IntX, IntY)<br/> If IntX > 0 And IntY > 0 Then<br/> MsgBox "此颜色在屏幕坐标为:x=" & IntX & " y=" & IntY & " 用时:" & (Timer - sint)<br/> Else<br/> MsgBox "查找某颜色坐标失败!!! 用时:" & (Timer - sint)<br/> End If</p><p><br/>End Sub<br/></p><p></p><p></p><p></p><p></p><p>'////////////////////////////////////////////////////////////////////////////////////////////////////////<br/>帮我看看哪里有问题?????语法之类的,,,</p><p></p>[此贴子已经被作者于2007-5-10 4:06:28编辑过] <p><font color="#00ff00">'szColor = (&HFF And szColor) * &H10000 + (&HFF00& And szColor) + (&HFF0000 And szColor) / &H10000</font></p><p><font color="#00ff00">这句还是要</font></p><p><font color="#00ff00">然后就是输入的参数</font></p><p>Call FindColor(1, 1, 1023, 767, 255, IntX, IntY)</p><p>要纯蓝,应该用RGB函数.</p><p>Call FindColor(1, 1, 1023, 767, RGB(0,0,255), IntX, IntY)</p> 现在主要是 蓝色那快代码..我可能写错了,,根本找不到颜色....坐标 <p>但代码在我这里没错</p><p>另外,你确认你要找的颜色是纯蓝??</p><p>RGB(0,0,255)就是纯蓝</p><p>你用QQ的截图功能看看颜色啊.</p> <p>大马..你用那代码可以找到,,蓝色坐标????</p><p></p><p>提示什么????</p><p></p><p>我这边一直提示找不到...</p> <p>也许你的颜色并不是纯蓝(0,0,255).</p><p>建议先用找色软件看看要找的点的颜色到底是什么样的RGB值(QQ的截图功能就可以看到)</p> <p>就是算是白色也找不到...</p><p>我的意思是,,蓝色那段代码有问题...帮我改改..或者告诉我</p><p></p><p><font color="#0000ff">BMPbuf(r) 这个数据的存储方式....</font></p> <p>我想这里有你需要的东西:</p><p><strong><font size="4">BMP图像格式解析</font></strong></p>http://blog.csdn.net/kesalin/archive/2006/09/12/1213163.aspx
页:
[1]