Im getting this error when trying to run a program in creating, how shold i interpet the error message, the macro runs just fine for many loops but then just suddely it breaks, giving this error.
************** Exception Text **************
System.ArgumentException: Value of '-1' is not valid for 'blue'. 'blue' should be greater than or equal to 0 and less than or equal to 255.
at System.Drawing.Color.CheckByte(Int32 value, String name)
at System.Drawing.Color.FromArgb(Int32 alpha, Int32 red, Int32 green, Int32 blue)
at System.Drawing.Color.FromArgb(Int32 red, Int32 green, Int32 blue)
at Dispatcher_Tool.ColorCheck.GetPixelAtCursor()
at Dispatcher_Tool.ColorCheck.getPixel()
at Dispatcher_Tool.ColorCheck.checkColorBlack(Int32 blackCordsX, Int32 blackCordsY)
at Dispatcher_Tool.main_normal.checkColor()
at Dispatcher_Tool.main_normal.startMacro(TextBox valX)
at Dispatcher_Tool.main_normal.button5_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
code around this point is,
[DllImport("gdi32")]
private static extern int GetPixel(IntPtr hdc, int x, int y);
[DllImport("User32")]
private static extern IntPtr GetWindowDC(IntPtr hwnd);
#region Pixel color test
private static readonly IntPtr DesktopDC = GetWindowDC(IntPtr.Zero);
public static System.Drawing.Color GetPixelAtCursor()
{
System.Drawing.Point p = Cursor.Position;
int color = GetPixel(DesktopDC, p.X, p.Y);
return System.Drawing.Color.FromArgb(color & 0xFF, color >> 8 & 0xFF, color >> 16);
}
The error message is fairly clear – you’re calling
Color.FromArgb, but you’re giving it a “blue” value of -1, which is invalid. From the docs:Quite how you fix that will depend on what your code is trying to do.
EDIT: Okay, now that you’ve posted the code I strongly suspect it’s returning CLR_INVALID, which I’m guessing is the bit pattern for -1 (i.e. all bits set). You’re just shifting that, which is being sign-extended so you’re still just getting -1.
It’s very easy to avoid this causing an exception – just mask the blue value in the same way you’re masking the others:
However, that’s really just going to hide the problem – you’ll end up with white where really you don’t have a valid value. You should potentially check whether
color == -1and act appropriately. Again, that exact behaviour will depend on your application.