GetPixel返回不正确的值 [英] GetPixel returns incorrect values

查看:81
本文介绍了GetPixel返回不正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在该论坛上的第一篇文章,如果我没有在正确的位置发布它,或者如果我做错了什么,请不要对我加倍努力,我不会在论坛上发布太多东西.因此,我在使用GetPixel函数时遇到了这个问题.基本上,它应该在x,y处返回颜色十进制.我要发布的代码可以在Windows 7 32bit上完美运行,但是最近我购买了带有Windows 8.1 64bit的新笔记本电脑y50-70,相同的代码完全不同.我找不到该问题的任何解决方案,甚至无法描述它.我认为这可能与台式机手柄,HDC,GetDC(),GetPixel()有关,甚至与我的计算机分辨率,刷新率之类的东西有关……我什至还录制了一些视频,可以帮助您了解我遇到的问题是因为我什至无法正确描述它.就像真实的颜色是x = 219,y = 407远离我的鼠标所指向的位置.新笔记本电脑已经使用了3周,我什至试图进行系统还原1次,但这并不能解决问题.

This is my first post on this forum, please don't go hard on me if I didn't post it in the right place or if I did something wrong, I don't post in forums very much. So, I have this problem with GetPixel function. Basically, it should return the color decimal at x, y. The code I am about to post works perfectly on windows 7 32bit, but recently I bought a new laptop y50-70 with windows 8.1 64bit and the same code works completely different. I can't find any solution to the problem, can't even describe it. I think it could have something to do with desktop handle, HDC, GetDC(), GetPixel(), maybe even with my computer resolution, refresh rate or something like that... I have even recorded some videos which could help you understand the problem I am having because I can't even describe it correctly. It is like the real color is x = 219, y = 407 away from the place, where my mouse is pointing. The new laptop is 3 weeks old, I even tried to do system restore 1 time, but that didn't solve the problem.

随时使用此代码,希望它对您有用:

Feel free to use this code, hope it will work fine for you:

#include <iostream>
#include <Windows.h>
using namespace std;

void Detect();

int main()
{
    Detect();

    return 0;
}

void Detect()
{
    POINT p;
    HDC hDC = GetDC(0);
    int x, y;

    while (!GetAsyncKeyState(VK_INSERT)) // Press insert to stop
    {
        GetCursorPos(&p);
        x = p.x;
        y = p.y;
        hDC = GetDC(0);
        cout << x << " " << y << " " << GetPixel(hDC, x, y) << endl;
        Sleep(50);
    }
    ReleaseDC(0, hDC);
}

链接到以下问题: https://youtu.be/q2H2M8WLHVI https://youtu.be/UcneHwXaGoM

如果任何人至少可以以某种方式提供帮助或说出要做什么,去哪里,我将非常非常感激.我开始编程的主要原因之一是因为这样的事情,需要处理颜色,条件等,现在我无法继续前进,这真的很可悲.希望能听到答复.谢谢.

If anyone could at least help somehow or tell what to do, where to go, I would very, very much appreaciate it. One of the main reasons why I started programming is because something like this, working with colours, conditions, etc... and now I can't advance further which is really sad. Hope to hear a reply. Thank you.

推荐答案

这可能与DPI缩放有关.

This is probably an issue with DPI scaling.

如果新显示器的每英寸点数高于平均水平,则Windows默认情况下将拉伸图形.默认情况下,Windows假定程序忽略DPI.如果Windows没有拉伸图形,则没有针对DPI进行调整的程序将在高密度显示器上的小窗口中显示小文本.

If your new monitor has a higher-than-average number of dots per inch, then Windows, by default, will stretch the graphics. By default, Windows assumes that programs ignore the DPI. If Windows didn't stretch the graphics, then programs that didn't adjust for the DPI would have tiny text in tiny windows on high-density displays.

这有点骇人听闻.让您询问显示器和窗口的某些Windows API和消息将转换平均"每英寸96像素与监视器的实际DPI之间的坐标.同样,让您确定大小的API也会执行相反的转换.因此,这对于程序来说基本上是透明的.但这并不是完美的,因为并非所有的API都能以一致的方式进行扩展.

This is a bit of a hack. Some of the Windows APIs and messages that let you ask about the display and windows will translate the coordinates between an "average" 96 pixels-per-inch and whatever the actual DPI is for the monitor. Likewise, the APIs that let you size things do the opposite transformation. So this is all largely transparent to the program. But it's not perfect because not all the APIs can do the scaling in a consistent manner.

因此,我的猜测是您的笔记本电脑具有高分辨率显示屏,GetPixel不会转换DPI缩放的坐标,而鼠标位置 会转换为DPI缩放.结果是您要求的像素实际上与鼠标不对齐.

So my guess is that your laptop has a high-resolution display, GetPixel doesn't translate coordinates for DPI scaling, and the mouse position is transformed for DPI scaling. The result is that you're asking for a pixel that doesn't really line up with the mouse.

我建议的解决方案是告诉窗口您的程序支持DPI".有几种方法可以做到这一点.在您的情况下,最简单的方法可能是调用

My suggested solution is the tell windows that your program is "DPI-aware". There are several ways to do this. The easiest in your case might be to call SetProcessDPIAware right at the beginning of your program. You could also do this be marking your program in the manifest. Depending on the compiler you're using, there might be a command line option to create the manifest you need automatically.

这篇关于GetPixel返回不正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆