C# 尝试在C中的控件边缘绘制类似Dropshadow的渐变

C# 尝试在C中的控件边缘绘制类似Dropshadow的渐变,c#,winforms,graphics,system.drawing,C#,Winforms,Graphics,System.drawing,我正在尝试创建一个函数,该函数将在控件的任何给定边上生成类似dropshadow的渐变。我对System.Drawing不太熟悉,但以前用过一次,并利用这一经验来尝试制作这个。我基本上画了一个渐变,一个像素线一个像素线通过一个for循环,这个循环使用一个叫做length的值来确定线的数量,它根据我使用的笔的alpha值来改变 问题肯定是我没有正确计算下一行的alpha值,或者我使用的System.Drawing没有正确。我想可能是后者,;无论是使用Graphics Graphics=this.C

我正在尝试创建一个函数,该函数将在控件的任何给定边上生成类似dropshadow的渐变。我对System.Drawing不太熟悉,但以前用过一次,并利用这一经验来尝试制作这个。我基本上画了一个渐变,一个像素线一个像素线通过一个for循环,这个循环使用一个叫做length的值来确定线的数量,它根据我使用的笔的alpha值来改变

问题肯定是我没有正确计算下一行的alpha值,或者我使用的System.Drawing没有正确。我想可能是后者,;无论是使用Graphics Graphics=this.CreateGraphics还是通过设置位图中的图形,我都不知道如何获取要显示的位图

我不确定如何分解这个问题的代码,但无论如何,这是一个相对较短的函数。有关函数应该如何工作的更多详细信息,请参阅文档

/// <summary>
/// Generates a simple drop shadow at any rectangular angle along the edge of a control of choice.
/// </summary>
/// <param name="startAlpha">The starting alpha (transparency) value of the gradient of the shadow.</param>
/// <param name="fourthsRotationDirection">The rotation (in 90 degree values) of the shadow.</param>
/// <param name="control">The control to be given a shadow.</param>
/// <param name="length">The length or depth of the shadow.</param>
/// <param name="clearShadow">Determines whether or not the function should clear all graphics to rid of any preexisting shadows, mostly for regeneration purposes.</param>
void DropShadowGenerator(int startAlpha, FourthsRotationDirection fourthsRotationDirection, Control control, int length, bool clearShadow)
{
    PictureBox image = new PictureBox();
    image.Location = new Point(0, 0);
    image.Size = this.Size;
    this.Controls.Add(image);

    Bitmap bmp = new Bitmap(this.Width, this.Height);
    Graphics graphics = Graphics.FromImage(bmp);
    Pen pen = (fourthsRotationDirection == FourthsRotationDirection.Zero || fourthsRotationDirection == FourthsRotationDirection.OneEighty) ? new Pen(Color.FromArgb(startAlpha), control.Height) : new Pen(Color.FromArgb(startAlpha), control.Width);
    Point startPoint = new Point();
    Point endPoint = new Point();
    float shadowFactor = 255 / length;
    float currentAlpha = startAlpha;

    if (clearShadow)
    {
        graphics.Clear(Color.Transparent);
    }

    if (fourthsRotationDirection == FourthsRotationDirection.Zero)
    {
        startPoint = new Point(control.Location.X + control.Width, control.Location.Y);
        endPoint = new Point(startPoint.X, startPoint.Y + control.Height);
    }
    else if (fourthsRotationDirection == FourthsRotationDirection.OneEighty)
    {
        startPoint = new Point(control.Location.X, control.Location.Y);
        endPoint = new Point(startPoint.X, startPoint.Y + control.Height);
    }
    else if (fourthsRotationDirection == FourthsRotationDirection.Ninety)
    {
        startPoint = new Point(control.Location.X, control.Location.Y);
        endPoint = new Point(startPoint.X + control.Width, startPoint.Y);
    }
    else if (fourthsRotationDirection == FourthsRotationDirection.TwoSeventy)
    {
        startPoint = new Point(control.Location.X, control.Location.Y + control.Height);
        endPoint = new Point(startPoint.X + control.Width, startPoint.Y);
    }

    for (int i = 1; i <= length; i++)
    {            
        graphics.DrawLine(pen, startPoint, endPoint);

        currentAlpha = currentAlpha - shadowFactor;
        pen.Color = Color.FromArgb((int)currentAlpha, Color.Black);

        switch (fourthsRotationDirection)
        {
            case FourthsRotationDirection.Zero:
                startPoint = new Point(startPoint.X++, startPoint.Y);
                endPoint = new Point(endPoint.X++, endPoint.Y);
                break;

            case FourthsRotationDirection.OneEighty:
                startPoint = new Point(startPoint.X--, startPoint.Y);
                endPoint = new Point(endPoint.X--, endPoint.Y);
                break;

            case FourthsRotationDirection.Ninety:
                startPoint = new Point(startPoint.X, startPoint.Y--);
                endPoint = new Point(endPoint.X, endPoint.Y--);
                break;

            case FourthsRotationDirection.TwoSeventy:
                startPoint = new Point(startPoint.X, startPoint.Y++);
                endPoint = new Point(endPoint.X, endPoint.Y++);
                break;
        }
    }

    image.Image = bmp;
}

我怎样才能让它显示在我的表格上?如前所述,我只是使用System.Drawing不正确,还是渐变生成本身不正确?

您的主要问题是计算新坐标的方式:

startPoint = new Point(startPoint.X++, startPoint.Y);
endPoint = new Point(endPoint.X++, endPoint.Y);
这实际上并不会改变坐标,因为您先指定值,然后再增加值。 尝试:

或者,我觉得更具可读性:

startPoint = new Point(startPoint.X+1, startPoint.Y);
endPoint = new Point(endPoint.X+1, endPoint.Y);
或者在不创建新点的情况下,可以执行以下操作:

startPoint.X++;
endPoint.X++;

建议:停止尝试重新发明轮子。它已经被发明了。使用WPF,您可以在两行XAML中完成这项工作,而不是对抗winforms的不可用性。
startPoint.X++;
endPoint.X++;