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 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了