tags:

views:

108

answers:

4

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);
    }
A: 

It seems pretty strait forward to me, -1 is being passed in to the CheckByte method, with a name "blue" and it doesnt like it.

Make sure you are not passing in negative values to your function

Mark
+2  A: 

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:

blue
Type: System.Int32
The blue component value for the new Color. Valid values are 0 through 255.

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:

return Color.FromArgb(color & 0xFF, (color >> 8) & 0xFF, (color >> 16) & 0xFF);

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 == -1 and act appropriately. Again, that exact behaviour will depend on your application.

Jon Skeet
i have added the code around that spot
Darkmage
Perhaps replace that function call with a call to Color.FromArgb(int argb). Also, check for -1 before calling it. The documentation: http://msdn.microsoft.com/en-us/library/2zys7833.aspx
Kevin Thiart
thanks for helping ill try to fix this based on your information.
Darkmage
A: 

Are you manipulating the color of certain element/control? The blue value of the RGB set fell out of the acceptable range of 0 to 255.

o.k.w
A: 

You are trying to create a color using Color.FromArgb, and passing -1 to the blue parameter, which is an illegal value.

Konamiman