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

时间:2015-10-10 05:57:11

标签: c# winforms custom-controls gdi+

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

}

1 个答案:

答案 0 :(得分:8)

使用光环或光环绘制文字

鲍勃鲍威尔(不幸的是site失败了)

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

以下是它的工作原理:

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

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

    <强>截图:

    enter image description here