下面是一个屏幕找图的源代码 使用的是GetPixel取色对比,平常用GetPixel去一个点的色还不觉的 但是在循环使用时就太慢了,我全屏幕找图 如果小图在右下角 起码要十分钟才能完成程序,
郁闷啊,在网上搜寻很久 有人说可以使用GetDIBits来截图把图放在内存中 代替GetPixel,我试了很久都不得其法,最终还是舔着脸来求助大家了bool FindPic(int _x1,int _y1,int _x2,int _y2, CString FileName, double s ,int &re_x,int &re_y)//FindPic(屏幕左上角坐标x,屏幕左上角坐标y,屏幕右下角X,屏幕右下角Y,图片路径,相似度,返回的图片X坐标,返回的图片Y坐标){ CBitmap bmp; BITMAP bm; HBITMAP hBmp; CDC bmpDC; hBmp = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),FileName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE); bmp.DeleteObject(); bmp.Attach( hBmp ); bmp.GetObject(sizeof(BITMAP),&bm); bmpDC.CreateCompatibleDC(NULL); bmpDC.SelectObject(&bmp); //图片DC HDC hScreenDC = ::GetDC(NULL); //屏幕DC bool found = false;//是否匹配到 bool next = false;//是否找下一个点 int width = bm.bmWidth; int height = bm.bmHeight; int limit = (double)(width * height) * (1-s); int count = 0; for (int i = _x1; i < (_x2 - width) && !found ; i++) { for (int j = _y1 ; j < (_y2 - height) && !found ; j++) { next = false; count = 0; for (int a = 0;a<width && !next;a++) { for (int b = 0;b<height &&!next;b++) { if(GetPixel(hScreenDC,i+a,j+b) != bmpDC.GetPixel(a, b))//这里 请问如何GetPixel转换成GetDIBits,使用GetPixel取色在循环里面太慢了 { //next = true;//找屏幕中下一个点 count ++; } if (count > limit) { next = true; } } } if (!next)//找到点 { found = true; re_x = i; re_y = j; return true; break; } } } return false;}
郁闷啊,在网上搜寻很久 有人说可以使用GetDIBits来截图把图放在内存中 代替GetPixel,我试了很久都不得其法,最终还是舔着脸来求助大家了bool FindPic(int _x1,int _y1,int _x2,int _y2, CString FileName, double s ,int &re_x,int &re_y)//FindPic(屏幕左上角坐标x,屏幕左上角坐标y,屏幕右下角X,屏幕右下角Y,图片路径,相似度,返回的图片X坐标,返回的图片Y坐标){ CBitmap bmp; BITMAP bm; HBITMAP hBmp; CDC bmpDC; hBmp = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),FileName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE); bmp.DeleteObject(); bmp.Attach( hBmp ); bmp.GetObject(sizeof(BITMAP),&bm); bmpDC.CreateCompatibleDC(NULL); bmpDC.SelectObject(&bmp); //图片DC HDC hScreenDC = ::GetDC(NULL); //屏幕DC bool found = false;//是否匹配到 bool next = false;//是否找下一个点 int width = bm.bmWidth; int height = bm.bmHeight; int limit = (double)(width * height) * (1-s); int count = 0; for (int i = _x1; i < (_x2 - width) && !found ; i++) { for (int j = _y1 ; j < (_y2 - height) && !found ; j++) { next = false; count = 0; for (int a = 0;a<width && !next;a++) { for (int b = 0;b<height &&!next;b++) { if(GetPixel(hScreenDC,i+a,j+b) != bmpDC.GetPixel(a, b))//这里 请问如何GetPixel转换成GetDIBits,使用GetPixel取色在循环里面太慢了 { //next = true;//找屏幕中下一个点 count ++; } if (count > limit) { next = true; } } } if (!next)//找到点 { found = true; re_x = i; re_y = j; return true; break; } } } return false;}
这句 拜托