我試圖創建將產生一個控制的任何給定的邊緣的陰影效果般的梯度功能。我對System.Drawing
並不是很熟悉,但之前曾經使用過它,並使用過這種體驗來嘗試做到這一點。我基本上繪製由一個像素線的梯度,一個像素的線條(通過一個for循環,使用稱爲length
確定的行數的值),即基於一種pen
我使用的alpha
值的變化。試圖繪製陰影效果,像梯度上控制的邊緣,C#
這個問題肯定是我錯誤地計算了如何獲得下一行的alpha
值,或者我錯誤地使用System.Drawing
。我認爲這可能是後者;我無法弄清楚如何使用Graphics graphics = this.CreateGraphics()
或從bitmap
設置graphics
得到bitmap
我生成顯示,無論是。
我不知道我怎麼會分手這個問題的代碼,但它是一個相對較短的功能進不去。有關函數如何工作的更多詳細信息,請參閱文檔。
/// <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,或者是我的漸變生成本身不正確?
建議:停止嘗試重新發明輪子。它已經被髮明瞭。使用WPF,你可以在2行XAML中完成這一步,而不是與winform不兼容。 –