Source/NFX.WinForms/Utils.cs
/// <summary>
/// Draws hand-drawn-looking highlighter color stick lines on top of text. The text itself is not drawn.
/// Text may span multiple lines
/// </summary>
/// <param name="gr">Graphical context</param>
/// <param name="rect">Bounding rectangle</param>
/// <param name="font">Font to measure text in</param>
/// <param name="text">Text to measure, the text is not drawn rather charecters position is determined</param>
/// <param name="align">Text alignment</param>
/// <param name="color">Color of highlighter including opacity</param>
Draws hand-drawn-looking highlighter color stick lines on top of text. The text itself is not drawn.
Text may span multiple lines
Draws hand - drawn - looking highlighter color stick lines on top of text . The text itself is not drawn . Text may span multiple lines
public static void DrawTextHighlight(Graphics gr,
                     Rectangle rect,
                     Font font,
                     string text,
                     StringAlignment align,
                     Color color)
    {
      Rectangle textRect;
      int lineHeight;
      using (StringFormat fmt = new StringFormat())
      {
        fmt.Alignment = align;
        SizeF size = gr.MeasureString(text, font, new SizeF(rect.Width, rect.Height), fmt);
        lineHeight = (int)gr.MeasureString("`~_W", font).Height;
        if (
            (align == StringAlignment.Near && Thread.CurrentThread.CurrentUICulture.TextInfo.IsRightToLeft) ||
            (align == StringAlignment.Far)
           )
          textRect = new Rectangle(rect.Right - (int)size.Width,
                                   rect.Top,
                                   (int)size.Width, (int)size.Height);
        else
          if (align == StringAlignment.Center)
            textRect = new Rectangle(rect.Left + (rect.Width / 2) - (int)size.Width / 2,
                                    rect.Location.Y,
                                    (int)size.Width, (int)size.Height);
          else
            textRect = new Rectangle(rect.Left, rect.Top, (int)size.Width, (int)size.Height);
      }
      textRect.Inflate(lineHeight, 0);
      if (textRect.Left < rect.Left) textRect.X = rect.Left;
      if (textRect.Right > rect.Right) textRect.Width -= textRect.Right - rect.Right;
      Rectangle brRect = textRect;
      brRect.Inflate(lineHeight / 2, lineHeight / 2);
      using (LinearGradientBrush br =
                    new LinearGradientBrush(brRect,
                    color,
                    Color.FromArgb(16, Color.Silver),
                    (align == StringAlignment.Far) ? 180f : 360f))
      {
        gr.SmoothingMode = SmoothingMode.AntiAlias;
        Random rnd = new Random();
        int y = textRect.Top + lineHeight / 2;
        Point[] pnt = new Point[4];
        while (y  < textRect.Bottom)
        {
          pnt[0] = new Point(textRect.Left, y - lineHeight / 2);
          pnt[1] = new Point(textRect.Right - lineHeight / 2, y - lineHeight / 2);
          pnt[2] = new Point(textRect.Right - lineHeight, y + lineHeight / 2);
          pnt[3] = new Point(textRect.Left + lineHeight / 2, y + lineHeight / 2);
          for (int i = 0; i < pnt.Length; i++)
            pnt[i].Offset(rnd.Next(0, 2), rnd.Next(-2, 2));
          gr.FillClosedCurve(br, pnt, FillMode.Alternate, 0.07f + (float)(rnd.NextDouble() / 6d));
          y += lineHeight + 2;
        }
      }
    }
public static void DrawTextHighlight ( Graphics gr , Rectangle rect , Font font , string text , StringAlignment align , Color color ) { Rectangle textRect ; int lineHeight ; using ( StringFormat fmt = new StringFormat ( ) ) { fmt . Alignment = align ; SizeF size = gr . MeasureString ( text , font , new SizeF ( rect . Width , rect . Height ) , fmt ) ; lineHeight = ( int ) gr . MeasureString ( " " , font ) . Height ; if ( ( align == StringAlignment . Near && Thread . CurrentThread . CurrentUICulture . TextInfo . IsRightToLeft ) || ( align == StringAlignment . Far ) ) textRect = new Rectangle ( rect . Right - ( int ) size . Width , rect . Top , ( int ) size . Width , ( int ) size . Height ) ; else if ( align == StringAlignment . Center ) textRect = new Rectangle ( rect . Left + ( rect . Width / 2 ) - ( int ) size . Width / 2 , rect . Location . Y , ( int ) size . Width , ( int ) size . Height ) ; else textRect = new Rectangle ( rect . Left , rect . Top , ( int ) size . Width , ( int ) size . Height ) ; } textRect . Inflate ( lineHeight , 0 ) ; if ( textRect . Left < rect . Left ) textRect . X = rect . Left ; if ( textRect . Right > rect . Right ) textRect . Width -= textRect . Right - rect . Right ; Rectangle brRect = textRect ; brRect . Inflate ( lineHeight / 2 , lineHeight / 2 ) ; using ( LinearGradientBrush br = new LinearGradientBrush ( brRect , color , Color . FromArgb ( 16 , Color . Silver ) , ( align == StringAlignment . Far ) ? 180f : 360f ) ) { gr . SmoothingMode = SmoothingMode . AntiAlias ; Random rnd = new Random ( ) ; int y = textRect . Top + lineHeight / 2 ; Point [ ] pnt = new Point [ 4 ] ; while ( y < textRect . Bottom ) { pnt [ 0 ] = new Point ( textRect . Left , y - lineHeight / 2 ) ; pnt [ 1 ] = new Point ( textRect . Right - lineHeight / 2 , y - lineHeight / 2 ) ; pnt [ 2 ] = new Point ( textRect . Right - lineHeight , y + lineHeight / 2 ) ; pnt [ 3 ] = new Point ( textRect . Left + lineHeight / 2 , y + lineHeight / 2 ) ; for ( int i = 0 ; i < pnt . Length ; i ++ ) pnt [ i ] . Offset ( rnd . Next ( 0 , 2 ) , rnd . Next ( - 2 , 2 ) ) ; gr . FillClosedCurve ( br , pnt , FillMode . Alternate , 0.07f + ( float ) ( rnd . NextDouble ( ) / 6d ) ) ; y += lineHeight + 2 ; } } }
Draws hand-drawn-looking highlighter color stick lines on top of text.
Draws hand - drawn - looking highlighter color stick lines on top of text .
//RIGHT //LEFT //gets rid of brush wrap //gr.DrawRectangle(Pens.Red, textRect); /*+ lineHeight / 2*/ //1 px padding top and bottom //using brush
gr
Graphics
None
rect
Rectangle
None
font
Font
Font to measure text in
Font to measure text in
text
string
Text to measure, the text is not drawn rather charecters position is determined
Text to measure the text is not drawn rather charecters position is determined
align
StringAlignment
None
color
Color
Color of highlighter including opacity
Color of highlighter including opacity