Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's 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 its 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!

Hi all,

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);
ReleaseDC(control.Handle, hDC);
}

Comments

  • Josh CodeJosh Code Posts: 675Member
    Check the colour depth settings on the computer.

    If any of the computers uses 16-bit colour depth, some little changes will happen to the colours due to not being stored to the same precision.

    : Hi all,
    :
    : 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);
    : ReleaseDC(control.Handle, hDC);
    : }
    :
  • ashesmanashesman Posts: 4Member
    So how do I programmaticly determine if the computer is running in 16 bit mode. I suppose I could limit the selection of colours to 16 bit. Without checking I am 99% sure my work PC was running in true color mode... I will look into this in a few days.

    Another thought I had was to do with the drawing functions. I assume the Graphics class does some clever stuff when it draws to create softer effects. Any ideas on this?


    : Check the colour depth settings on the computer.
    :
    : If any of the computers uses 16-bit colour depth, some little
    : changes will happen to the colours due to not being stored to the
    : same precision.
    :
    : : Hi all,
    : :
    : : 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);
    : : ReleaseDC(control.Handle, hDC);
    : : }
    : :
    :

Sign In or Register to comment.