Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.
GetPixel color is slightly wrong!
I am writing a C# .net data plotting application. I use Graphics.DrawLine() to draw my pretty plot lines. Then in the MouseMove event I use a call to windows API GetPixel() (code below) to read the pixel under the mouse. If it is the same color as one of the plot lines I highlight that line next time it is drawn... pretty simple stuff!
The problem is that this works perfectly on my home PC, but on my work PC there is a discrepancy between the colour of the drawn line and the colour I read using GetPixel(). I am not sure if the line is drawn with the wrong color, or the wrong color is read! Eg I write a colour like A=255, R=255, G=128, B= 128 and I read a colour like A=255, R=255, G=132, B=132. There is no problem with standard colours like white, red, black etc...
Any help would be greatly appreciated...
Color color = Color.Empty;
if (control != null)
IntPtr hDC = GetDC(control.Handle);
int colorRef = GetPixel(hDC, x, y);
color = Color.FromArgb(
(int)(colorRef & 0x000000FF),
(int)(colorRef & 0x0000FF00) >> 8,
(int)(colorRef & 0x00FF0000) >> 16);
//color = ColorTranslator.FromWin32(colorRef);