Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

GetPixel(hdc, int, int)

luckyboyluckyboy Member Posts: 105
hi every one, i try to get the color under mouse cursor and i used this code but every time i run it i get "Error" which means that COLORREF clr = COLOR_INVALID
Why it always set to COLOR_INVALID ???
same happened when i switched to CDC::GetPixel(int, int)
[code]
void CColorDlg::OnOK()
{
// TODO: Add extra validation here
LPPOINT lpPoint = new POINT;
BOOL gotPos = GetCursorPos(lpPoint);

if(gotPos) {

CDC *cdc = GetDC();
COLORREF clr = cdc->GetPixel(*lpPoint);

if(clr == CLR_INVALID)
MessageBox("Error");

else {

m_red = clr & 0xFF;
clr >>= 0x08;
m_green = clr & 0xFF;
clr >>= 0x08;
m_blue = clr & 0xFF;
UpdateData(false);

}

}
delete lpPoint;
//CDialog::OnOK();
}
[/code]
M. Nasim

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : hi every one, i try to get the color under mouse cursor and i used
    : this code but every time i run it i get "Error" which means that
    : COLORREF clr = COLOR_INVALID
    : Why it always set to COLOR_INVALID ???
    : same happened when i switched to CDC::GetPixel(int, int)
    : [code]:
    : void CColorDlg::OnOK()
    : {
    : // TODO: Add extra validation here
    : LPPOINT lpPoint = new POINT;
    : BOOL gotPos = GetCursorPos(lpPoint);
    :
    : if(gotPos) {
    :
    : CDC *cdc = GetDC();
    : COLORREF clr = cdc->GetPixel(*lpPoint);
    : [color=Red][b]ReleaseDC (cdc);[/b][/color]
    :
    : if(clr == CLR_INVALID)
    : MessageBox("Error");
    :
    : else {
    :
    : m_red = clr & 0xFF;
    : clr >>= 0x08;
    : m_green = clr & 0xFF;
    : clr >>= 0x08;
    : m_blue = clr & 0xFF;
    : UpdateData(false);
    :
    : }
    :
    : }
    : delete lpPoint;
    : //CDialog::OnOK();
    : }
    : [/code]:
    : M. Nasim
    [color=Blue]Few things:

    1. You have missed to release the HDC - see RED.

    2. You need to also convert the screen coordinates into client coordinates of a window you trying to get the pixel from.

    3. Why allocate the POINT with 'new'? - You can declare it as local variable, without abusing the heap allocator. Saves both code size and speed.

    4. Can you tell more about your dialog? Do you have some kind of control on it, which is painted with colors and then you try to get the color values from that control? If that is so, you have to use the DC from the control and you getting it from dialog itself. This code should do the trick:
    [/color]
    [code]
    POINT pos;
    GetCursorPos (&pos);

    CWnd* pControl = GetDlgItem (IDC_YOUR_COLOR_CONTROL);
    pControl -> ScreenToClient (&pos);

    CDC* pDC = pControl -> GetDC ();
    COLORREF rgb = pDC -> GetPixel (&pos);
    pControl -> ReleaseDC (pDC);
    [/code]
  • luckyboyluckyboy Member Posts: 105
    Thanks fro your interest, and i'm so sorry for my late response due to an international Internet connection issue.

    1- I released the DC and still have the same problem.
    2- my dialog contains only three text boxes and a button, and i intend to use this small program to retrieve color from any part of the screen.
    you know, some times you find some wonderful background color and you may need this color in you designs, it is too hard to get the same color in your design unless you get a color picker, and that is my program just a color picker.

    thanks in advance
    M. Nasim
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : Thanks fro your interest, and i'm so sorry for my late response due
    : to an international Internet connection issue.
    :
    : 1- I released the DC and still have the same problem.
    : 2- my dialog contains only three text boxes and a button, and i
    : intend to use this small program to retrieve color from any part of
    : the screen.
    : you know, some times you find some wonderful background color and
    : you may need this color in you designs, it is too hard to get the
    : same color in your design unless you get a color picker, and that is
    : my program just a color picker.
    :
    : thanks in advance
    : M. Nasim
    [color=Blue]Then it should be no problem to use the HDC from desktop to get the color of anything on the desktop. Try this code:[/color]
    [code]
    POINT pos;
    GetCursorPos (&pos);

    HDC hScreenDC = ::GetDC (NULL);
    COLORREF rgb = ::GetPixel (hScreenDC, pos.x, pos.y);
    ::ReleaseDC (NULL, hScreenDC);

    UINT red = GetRValue (rgb);
    UINT green = GetGValue (rgb);
    UINT blue = GetBValue (rgb);
    [/code]
  • luckyboyluckyboy Member Posts: 105
    Thanx for help
    it finally works
  • anilkumar23172anilkumar23172 Member Posts: 2
    hello sir
    i am also working on GetPixel() function in vc++
    sir i want ot calculate the intensity of pixel so will plz mail me ur full code so that i can get some idea from ur coding
    i hope u will help me
    thank u
    eid:anilkumar23172@gmail.com

  • luckyboyluckyboy Member Posts: 105
    thank you for looking for old threads instead of posting new ones about the same topic.
    i am sorry i can't help you, all my code was about retrieving RGB values with no intensity.
    and y the way, i didn't use GetPixel() shipped with VC++ (MFC). instead i used the global version that is part of win32 API.
    i don't know how to retrieve it. but i guess that the returned result contains this information in the last byte. since it returns 4-byte value, 1-Red, 2-Green, 3-Blue. so i guess that 4-intensity.
    ONLY GUESSING
    see ya.
  • anilkumar23172anilkumar23172 Member Posts: 2
    thank u very much sir


Sign In or Register to comment.