• 13
• 17
• 27
• 9
• 9

# Using GetPixel() -problems with output

This topic is 4934 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi. I'm using getpixel quite a bit and I'm having problems outputting text to the console window. For example:

HDC hdcDesktop2= GetDC(NULL);
COLORREF clr,clr2,clr3,clr4,clr5,clr6,clr7,clr8,clr9,clr10,clr11,clr12,
clr13,clr14,clr15,clr16,clr17,clr18,clr19;

// Check if your get pixel call will succeed.
if (GetPixel(hdcDesktop2, 100, 100) == CLR_INVALID) {
printf("Get pixel error!\n");
return 0;
}

clr = GetPixel(hdcDesktop2, 255+gtrans, 172+htrans); //
clr2 = GetPixel(hdcDesktop2, 254+gtrans, 169+htrans);
clr3 = GetPixel(hdcDesktop2, 252+gtrans, 167+htrans);
clr4 = GetPixel(hdcDesktop2, 249+gtrans, 166+htrans);
clr5 = GetPixel(hdcDesktop2, 251+gtrans, 169+htrans);
clr6 = GetPixel(hdcDesktop2, 249+gtrans, 171+htrans);
clr7 = GetPixel(hdcDesktop2, 249+gtrans, 175+htrans);
clr8 = GetPixel(hdcDesktop2, 251+gtrans, 178+htrans);
clr9 = GetPixel(hdcDesktop2, 252+gtrans, 176+htrans);

ReleaseDC(NULL,hdcDesktop2);

cout<<clr9;

cin.get();  ////won't print to screen without this command


Without the cin.get(), I don't get any output to the screen for some reason. This is only one small portion of my code. Is it possible that due to some loops in code above this code I didn't release a device context and so that is why I'm not getting the output ?

##### Share on other sites
I would guess that the cin.get( ) call causes an output buffer flush. Thus to get things working how you want you need to use std::endl like so:

std::cout << clr9 << std::endl;

##### Share on other sites
Thanks. Do you know what Include I use or what I put at the top? I'm getting errors using MSVC++ 6.0 . It says:

error C2653: 'std' : is not a class or namespace name

##### Share on other sites
Nevermind. Got it working thanks to you !!! Thanks man.

This turned out to be the winner for me:

cout <<clr17<<"\n"<< endl;

The endl at the end made it spit the output to the screen. Thanks. I think you gave me some MFC code but its all good thanks dude.