简体   繁体   English

C#软件调试问题中的错误消息

[英]Error message in C# software debug question

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. 错误消息非常清楚-您正在调用Color.FromArgb ,但您给它提供的“蓝色”值为-1,这是无效的。 From the docs: 从文档:

blue 蓝色
Type: System.Int32 类型:System.Int32
The blue component value for the new Color. 新颜色的蓝色分量值。 Valid values are 0 through 255. 有效值为0到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 (ie all bits set). 编辑:好的,现在您已经发布了代码,我强烈怀疑它正在返回CLR_INVALID,我猜这是-1的位模式(即所有位都已设置)。 You're just shifting that, which is being sign-extended so you're still just getting -1. 您只是在移动它,它正进行符号扩展,所以您仍然只得到-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. 您应该检查color == -1是否color == -1并采取适当措施。 Again, that exact behaviour will depend on your application. 同样,确切的行为将取决于您的应用程序。

You call Color.FromArgb() from GetPixelAtCursor() . 您从GetPixelAtCursor()调用Color.FromArgb() GetPixelAtCursor()

For some reason you have a negative value for (at least) the blue component. 由于某种原因,您(至少)蓝色成分的值为负。

Better show us some of the relevant code at that point. 此时最好向我们展示一些相关代码。

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. 对我来说,这似乎很困难,-1传递给CheckByte方法,名称为“ blue”,它不喜欢它。

Make sure you are not passing in negative values to your function 确保您没有向函数传递负值

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. RGB集的蓝色值超出了0到255的可接受范围。

您正在尝试使用Color.FromArgb创建颜色,并将-1传递给blue参数,这是非法值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM