本文介绍了自定义控件中文本的外发光效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c#winforms中的Label文本上应用外发光,以及模糊效果。使用自定义控件



如您所见,这是自定义面板,我正在尝试对整个文本进行发光效果。

 受保护的覆盖无效OnPaint(PaintEventArgs pe)
{
//base.OnPaint(pe);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;

GraphicsPath GP =新的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) ;

//使用(SolidBrush笔刷= new SolidBrush(BackColor))在边框

pe.Graphics.FillRectangle(brush,ClientRectangle);
pe.Graphics.DrawRectangle(新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);

}


解决方案

使用光环或光环绘制文本



感谢鲍勃·鲍威尔(Bob Powell)在


How to apply Outer Glow on text of Label in c# winforms, and Blur Effect. Using Custom Control

As you See, This is Custom Panel and I'm trying to do Glow Effect for the entire text.

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);

}
解决方案

Drawing text with a halo or aura

Thanks to Bob Powell for the great post on Text Halo Effect.(The original site is down)

The technique relies on drawing the text twice. Once to a shrunken bitmap which represents the halo, this will be expanded to full size using the interpolation mode of choice, and once at full size to create the actual text. The bitmap which is used to create the halo must be of a specific size ratio to the original text. In this case I have chosen a ratio of 1:5 so the halo text must be drawn at 1 fifth size.

Here's how it works:

  1. create a new bitmap that is smaller than your original drawing area by some fixed ratio. In this case 1/5th.
  2. create a GraphicsPath and had the desired text to it.
  3. obtain a graphics object for the bitmap and create a matrix that shrinks all drawing output by the chosen ratio.
  4. Fill the text path using the desired halo color and then, just for good measure, stroke the text path with a pen to provide a little bit of edge to the aura.
  5. Set the interpolation mode in the destination graphics object to HighQualityBilinear and stretch the bitmap containing the halo using the chosen ratio again.
  6. Finally, on the destination graphics object, fill the text path without changing the size. This should register the text correctly with the fuzzy outline of the halo and produce the final effect.

Code:

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();
}

Screenshot:

这篇关于自定义控件中文本的外发光效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 02:22