qq_42477894 2019-06-10 21:36 采纳率: 0%
浏览 997

C语言GetPixel函数获得的值不正确?

编写的图片识别程序,找到屏幕上的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也试了一下还是不行。

  • 写回答

1条回答 默认 最新

  • qq_42477894 2019-06-11 11:41
    关注

    DC的内容没有跟着屏幕更新,我把GetDC函数放到了main里之后一切就正常啦!不过还不太清楚原因是什么

    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);
        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 << "标志位"<<i<<"不匹配!" << 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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大