如何在c#winforms中的Label文本上应用外发光和模糊效果。使用自定义控件
如您所见,这是自定义面板,我正在尝试为整个文本进行发光效果。
protected override void OnPaint(PaintEventArgs pe)
{
//base.OnPaint(pe);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
GraphicsPath GP = new GraphicsPath();
GP.FillMode = FillMode.Alternate;
GP.AddString(this.Text, this.Font.FontFamily, 2, 12f, new Point(ClientRectangle.X+Text.Length*4-20, ClientRectangle.Y+10), sf);
// In Border
using (SolidBrush brush = new SolidBrush(BackColor))
pe.Graphics.FillRectangle(brush, ClientRectangle);
pe.Graphics.DrawRectangle(new Pen(Color.FromArgb(_InnerBorderColor.R, _InnerBorderColor.B, _InnerBorderColor.G), 1.0f), 0, 0, ClientSize.Width - 2, ClientSize.Height - 2);
pe.Graphics.DrawPath(new Pen(Color.Blue, 2f), GP);
pe.Graphics.DrawString(base.Text, this.Font, Brushes.Black, 2, 2);
}
答案 0 :(得分:8)
使用光环或光环绘制文字
鲍勃鲍威尔(不幸的是site失败了)
该技术依赖于两次绘制文本。一旦到了表示光晕的缩小的位图,这将使用所选的插值模式扩展到完整大小,并且在全尺寸下创建实际文本。用于创建光晕的位图必须与原始文本具有特定的大小比例。在这种情况下,我选择了1:5的比例,因此光晕文本必须以1/5大小绘制。
以下是它的工作原理:
<强>代码:强>
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//Create a bitmap in a fixed ratio to the original drawing area.
Bitmap bm=new Bitmap(this.ClientSize.Width/5, this.ClientSize.Height/5);
//Create a GraphicsPath object.
GraphicsPath pth=new GraphicsPath();
//Add the string in the chosen style.
pth.AddString("Text Halo",new FontFamily("Verdana"),(int)FontStyle.Regular,100,new Point(20,20),StringFormat.GenericTypographic);
//Get the graphics object for the image.
Graphics g=Graphics.FromImage(bm);
//Create a matrix that shrinks the drawing output by the fixed ratio.
Matrix mx=new Matrix(1.0f/5,0,0,1.0f/5,-(1.0f/5),-(1.0f/5));
//Choose an appropriate smoothing mode for the halo.
g.SmoothingMode=SmoothingMode.AntiAlias;
//Transform the graphics object so that the same half may be used for both halo and text output.
g.Transform=mx;
//Using a suitable pen...
Pen p=new Pen(Color.Yellow,3);
//Draw around the outline of the path
g.DrawPath(p,pth);
//and then fill in for good measure.
g.FillPath(Brushes.Yellow,pth);
//We no longer need this graphics object
g.Dispose();
//this just shifts the effect a little bit so that the edge isn't cut off in the demonstration
e.Graphics.Transform=new Matrix(1,0,0,1,50,50);
//setup the smoothing mode for path drawing
e.Graphics.SmoothingMode=SmoothingMode.AntiAlias;
//and the interpolation mode for the expansion of the halo bitmap
e.Graphics.InterpolationMode=InterpolationMode.HighQualityBicubic;
//expand the halo making the edges nice and fuzzy.
e.Graphics.DrawImage(bm,ClientRectangle,0,0,bm.Width,bm.Height,GraphicsUnit.Pixel);
//Redraw the original text
e.Graphics.FillPath(Brushes.Black,pth);
//and you're done.
pth.Dispose();
}
<强>截图:强>