Wednesday, February 06, 2008

 My previous job required a bit of multimedia programming and involved the use of a lot of different graphics techniques. One day we ran across a bit of code to give drawn text either an outerglow or an outline around it. It turns outs to be much simpler to do than I previously thought and only goes to show that you can always learn a new trick. The original project can be downloaded from FancyText.

The basic concept here is drawing the text multiple times in slightly different locations in one color with the alpha channel set to around 15 to 25. Next you print the original text centered back over the glowing text. The results look excellent. When I get the time I will create a label style control based on this code and post it. 

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.ComponentModel;
   4:  using System.Data;
   5:  using System.Drawing;
   6:  using System.Text;
   7:  using System.Windows.Forms;
   8:  using System.Drawing.Drawing2D;
   9:  using System.Drawing.Text;
  10:   
  11:  namespace OutGlowText
  12:  {
  13:      public partial class MainForm : Form
  14:      {
  15:          string OutStr = "Glowing Text";
  16:          int BlurAmt = 9;
  17:   
  18:          public MainForm()
  19:          {
  20:              InitializeComponent();
  21:          }
  22:   
  23:          private void MainForm_Load(object sender, EventArgs e)
  24:          {
  25:              this.BackColor = Color.Black;
  26:          }
  27:   
  28:          protected override void OnPaint(PaintEventArgs e)
  29:          {
  30:              Graphics g = e.Graphics;
  31:              Brush br = new SolidBrush(Color.FromArgb(15, Color.White));
  32:   
  33:              g.SmoothingMode = SmoothingMode.HighQuality;
  34:              g.InterpolationMode = InterpolationMode.HighQualityBilinear;
  35:              g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
  36:   
  37:              for (int x = 0; x <= BlurAmt; x++)
  38:              {
  39:                  for (int y = 0; y <= BlurAmt; y++)
  40:                  {
  41:                      g.DrawString(OutStr, new Font("Arial", 48,FontStyle.Bold),br, new Point(x, y));
  42:                  }
  43:              }
  44:              
  45:              g.DrawString(OutStr, 
  46:                           new Font("Arial", 48,FontStyle.Bold ), 
  47:                           Brushes.Blue, 
  48:                           new Point(BlurAmt /2, BlurAmt/2));
  49:   
  50:              base.OnPaint(e);
  51:          }
  52:   
  53:      }
  54:  }
 
kick it on DotNetKicks.com
posted on Wednesday, February 06, 2008 6:32:42 PM (Eastern Standard Time, UTC-05:00)  #    Comments [4]
Related posts:
Object Initializers
dnrTV Aggregator Part 2 - LINQ to XML
Create an Access Database in C#


Monday, February 04, 2008 11:14:07 PM (Eastern Standard Time, UTC-05:00)
Hey John!
I was unwilling to use the 3.0/3.5 .Net imports just so I can have this outline effect.
This code was exactly what I was after so that when I write onto an image I can assure the text will not blend with the background.

Thank you very much!!
anonymous
Tuesday, February 05, 2008 12:20:06 AM (Eastern Standard Time, UTC-05:00)
Glad to hear. I used to have the code put in a control, but misplaced it.
John Plant
Sunday, March 23, 2008 2:11:48 AM (Eastern Standard Time, UTC-05:00)
Thanks very much. I was able to use this for dynamic image generation for a web application. One of the requirements was to have the dynamically created text have an outline.

This was a huge advantage because I will be able to complete the project using .net instead of flash. This is great because I don't know flash!
Warren
Saturday, May 23, 2009 11:42:22 AM (Eastern Standard Time, UTC-05:00)
Could you help me. A minute's success pays the failure of years. Help me! Help to find sites on the: Turbo Tax. I found only this - <a href="http://turbo-tax.biz">turbo tax</a>. By contrast, the intersection of crime, alcohol problems, and treatment for alcohol problems offers unexpected opportunities for understanding the. Focus healthcare of tennessee is a jcaho accredited full service drug and alcohol treatment center in chattanooga, tennessee. With love ;-), Hattie from Islands.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):