Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 面板背景漆不起作用_C#_Winforms_Gradient_Paint - Fatal编程技术网

C# 面板背景漆不起作用

C# 面板背景漆不起作用,c#,winforms,gradient,paint,C#,Winforms,Gradient,Paint,我一直在用面板绘制winforms的边框。我想在底部面板中使用渐变,但我遇到了一个问题 我使用的方法几乎与我在这里绘制表单背景时使用的方法相同,但我在面板中得到了一个红十字。我不知道为什么会发生这种情况,它应该可以正常工作 图片: 这是我要绘制的代码: public void Colorear_Barra_abajo(object sender, PaintEventArgs e) { Rectangle r = this.ClientRectangle;

我一直在用面板绘制winforms的边框。我想在底部面板中使用渐变,但我遇到了一个问题

我使用的方法几乎与我在这里绘制表单背景时使用的方法相同,但我在面板中得到了一个红十字。我不知道为什么会发生这种情况,它应该可以正常工作

图片:

这是我要绘制的代码:

public void Colorear_Barra_abajo(object sender, PaintEventArgs e)
    {
        Rectangle r = this.ClientRectangle;

        if (r.Width > 0 && r.Height > 0)
        {
            Color c1 = Color.FromArgb(255, 54, 54, 54);
            Color c2 = Color.FromArgb(255, 98, 98, 98);

            LinearGradientBrush br = new LinearGradientBrush(r, c1, c2, 90, true);
            ColorBlend cb = new ColorBlend();
            cb.Positions = new[] { (float)0.357, (float)0.914 };
            cb.Colors = new[] { c1, c2 };
            br.InterpolationColors = cb;

            // paint
            e.Graphics.FillRectangle(br, r);
        }
    }
我是这样称呼它的(panel_Borde_abajo就是这个小组):

我曾经使用这种方法来绘制表单以外的其他控件(menustrip f.e),它们工作得很好,但这个特殊的控件不起作用

例如,该选项工作正常,并且没有出现红十字:

public void Form_Background(object sender, PaintEventArgs e)
    {
        Rectangle r = this.ClientRectangle;

        if (r.Width > 0 && r.Height > 0)
        {
            Color c1 = Color.FromArgb(255, 252, 254, 255);
            Color c2 = Color.FromArgb(255, 247, 251, 253);
            Color c3 = Color.FromArgb(255, 228, 239, 247);
            Color c4 = Color.FromArgb(255, 217, 228, 238);
            Color c5 = Color.FromArgb(255, 177, 198, 215);

            LinearGradientBrush br = new LinearGradientBrush(r, c1, c5, 90, true);
            ColorBlend cb = new ColorBlend();
            cb.Positions = new[] { 0, (float)0.3, (float)0.486, (float)0.786, 1 };
            cb.Colors = new[] { c1, c2, c3, c4, c5 };
            br.InterpolationColors = cb;

            // paint
            e.Graphics.FillRectangle(br, r);
        }
    }

如果有人能帮我,那就太好了

看起来,您对ColorBlend.Positions属性使用了无效值,请尝试以下代码:

ColorBlend cb = new ColorBlend();
cb.Positions = new[] { 0.0f, 0.357f, 0.914f, 1.0f };
                       ^^^^                  ^^^^
根据报告:

此数组中的元素由浮点值表示 介于0.0f和1.0f之间,且数组的第一个元素必须为0.0f 最后一个元素必须是1.0f


红十字会表示你的绘画方法出现了异常。如果使用debug+异常调试,请勾选CLR异常的“抛出”复选框。然后您将发现ColorBlend对象的问题。感谢您的回答,我现在必须再添加两种颜色,因此我在0中使用了相同的第一种颜色,并在1中重复了最后一种颜色。但是我没有得到和WPF相同的梯度。好消息是,至少这是有效的。术士,你解决了我的问题,但我有一个问题。我认为,当我使用“矩形r=this.ClientRectangle”时,它采用的是表单的大小,而不是我的面板。也许这就是我没有正确得到梯度的原因。是的,我认为,一个关键词“this”与形式有关。在本例中,“this.ClientRectangle”返回一个矩形,该矩形表示窗体的客户端区域(而不是面板)。是的,我实际上在那里得到了窗体的大小。我现在将其更改为面板大小并正常工作。再次感谢你!
ColorBlend cb = new ColorBlend();
cb.Positions = new[] { 0.0f, 0.357f, 0.914f, 1.0f };
                       ^^^^                  ^^^^