编写的图片识别程序,找到屏幕上的20个像素点然后和已保存的BMP文件作对比,每次GetPixel函数获得的值RGB均是255也就是白色,然后我把GetPixel函数单独拿出来对指定的窗口遍历又用SetPixel函数画了出来他的结果就是对的。
值正确的的代码如下:
#include <Windows.h>
#include <iostream>
#include <stdio.h>
using namespace std;
typedef struct
{
BYTE b;
BYTE g;
BYTE r;
}RGB;
HWND hnd;
HDC hdc;
HWND hnd1;
HDC hdc1;
COLORREF image1;
RGB *cutimage=NULL;
int CutImage(int PointX,int PointY)
{
cutimage=(RGB*)malloc(3);
COLORREF image;
image = GetPixel(hdc, PointX, PointY);
cutimage->b = GetBValue(image);
printf("b is:%d\t",cutimage->b);
cutimage->g = GetGValue(image);
printf("g is:%d\t",cutimage->g);
cutimage->r = GetRValue(image);
printf("r is:%d\t\n",cutimage->r);
SetPixel(hdc1,PointX+10,PointY+200,image);
return 1;
}
int main()
{
hnd1=FindWindow(NULL,"无标题 - 画图");
hdc = GetDC(hnd);
hdc1=GetDC(hnd1);
hnd=FindWindow(NULL,"阴阳师-网易游戏");
image1 = GetPixel(hdc, 0, 0);
SendMessage(hnd,MoveWindow(hnd,0,0,1000,570,true),0,0);
int a = 0;
system("pause");
for (int i=0;i<20;i=i+1)
{
cout<<i<<endl;
CutImage(405+i,297);
}
system("pause");
return 1;
}
值错误的代码如下:
#include "ExternType.h"
/*Test Start*/
RGB *BMP1 = (RGB*)malloc(60);
RGB *BMP2 = (RGB*)malloc(60);
RGB *BMP3 = (RGB*)malloc(60);
RGB *BMP4 = (RGB*)malloc(60);
RGB *cutimage = (RGB*)malloc(60);
RGB *F1 = BMP1;
RGB *F2 = BMP2;
RGB *F3 = BMP3;
RGB *F4 = BMP4;
RGB *F5 = cutimage;
HWND hnd;
HDC hdc;
/*Test End*/
int main()
{
system("pause");
int i = 0;
int PointX[4] = { 734,734,50,50 };
int PointY[4] = { 465,465,59,59 };
StartUp();
do
{
cout << "----------------------------" << endl;
CutImage(PointX[i], PointY[i]);
i++;
if (i == 4)
i = 0;
switch (Compares())
{
case 0: {cout << "没有匹配图片!" << endl; break; }
case 1: {cout << "等待玩家!" << endl; break; }
case 2: {cout << "点击开始!" << endl; break; }
case 3: {cout << "战斗结束1" << endl; break; }
case 4: {cout << "战斗结束2" << endl; break; }
}
} while (i != 5);
/*Test Start*/
/*Test End*/
system("pause");
free(F1);
free(F2);
free(F3);
free(F4);
free(F5);
return 1;
}
#include "ExternType.h"
extern RGB *cutimage;
extern HWND hnd;
extern HDC hdc;
int CutImage(int PointX,int PointY)
{
COLORREF image;
for (int i = 0; i < 20; i++)
{
image = GetPixel(hnd, PointX+i, PointY);
cutimage->b = GetBValue(image);
cutimage->g = GetGValue(image);
cutimage->r = GetRValue(image);
*cutimage++;
}
for (int j = 0; j < 20; j++)
*cutimage--;
return 1;
}
这个问题想了很久了,网上说需要创建一个兼容的DC也试了一下还是不行。