自定义控件中文本的外发光效果

Outer Glow Effect for Text in a Custom Control

如何在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);

}

绘制带有光环或光环的文字

感谢 Bob Powell post Text Halo Effect

该技术依赖于两次绘制文本。一旦是代表光环的缩小位图,将使用选择的插值模式将其扩展到全尺寸,一旦达到全尺寸以创建实际文本。用于创建光晕的位图必须与原始文本具有特定的大小比例。在这种情况下,我选择了 1:5 的比例,因此光晕文本必须以五分之一的尺寸绘制。

工作原理如下:

  1. 创建一个比原始绘图区域小一定比例的新位图。在这种情况下 1/5th.
  2. 创建一个 GraphicsPath 并向其添加所需的文本。
  3. 获取位图的图形对象并创建一个矩阵,按选定的比例缩小所有绘图输出。
  4. 使用所需的光晕颜色填充文本路径,然后,为了更好地衡量,用钢笔描边文本路径,为光环提供一点边缘。
  5. 将目标图形对象中的插值模式设置为 HighQualityBilinear 并再次使用所选比率拉伸包含光晕的位图。
  6. 最后,在目标图形对象上,在不改变大小的情况下填充文本路径。这应该可以正确地将文本与光晕的模糊轮廓对齐并产生最终效果。

代码:

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

截图: