Thursday, July 24, 2008

Outline border on Text using GDI+

·

Outline Border On Text Using GDI+

string text="Welcome to Asp.Net";
string fontFamily = "Impact";
float fontSize = 200;
bool fontBold = true;
bool fontItalic = true;
bool fontUnderline = false;
Color fontColor ="#FF0000";
Color b_Color ="#00FF00"; // Border Color
int BlurAmt =9;
SizeF size = new SizeF();
Font font = null;
Bitmap bitmap = new Bitmap(1, 1);
Graphics g = Graphics.FromImage(bitmap);

g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.SmoothingMode = SmoothingMode.HighQuality;


FontStyle fontStyle = FontStyle.Regular;
if (fontBold) fontStyle |= FontStyle.Bold;
if (fontItalic) fontStyle |= FontStyle.Italic;
if (fontUnderline) fontStyle |= FontStyle.Underline;

font = new Font(fontFamily, fontSize, fontStyle,GraphicsUnit.Pixel);
StringFormat sf = new StringFormat();

sf.Alignment = StringAlignment.Near;
sf.FormatFlags = StringFormatFlags.DisplayFormatControl;
sf.LineAlignment = StringAlignment.Near;
sf.Trimming = StringTrimming.Word;
size = g.MeasureString(text, font,new PointF(0,0),sf);
bitmap = new Bitmap((int)size.Width , (int)size.Height ,PixelFormat.Format32bppPArgb);

g = Graphics.FromImage(bitmap);
g.Clear(Color.Transparent);

sf.Trimming = StringTrimming.None;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
g.CompositingQuality = CompositingQuality.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;

GraphicsUnit gUnit = GraphicsUnit.Pixel;

SolidBrush br = new SolidBrush(Color.White);

//LinearGradientBrush br = new LinearGradientBrush(new RectangleF(0, 0, size.Width, size.Height), Color.Red, Color.Yellow, LinearGradientMode.Horizontal);

br = new SolidBrush(fontColor);
SolidBrush b_color = new SolidBrush(Color.White);
b_color = new SolidBrush(b_Color);
for (int x = 0; x <= BlurAmt; x++)
{
for (int y = 0; y <= BlurAmt; y++)
{
g.DrawString(text, font, b_color, new Point(x, y), sf);
}
}
g.DrawString(text, font, br, new Point(BlurAmt / 2 , BlurAmt / 2 ), sf);

MemoryStream m = new MemoryStream();

bitmap.Save(m, ImageFormat.Png);
m.WriteTo(Response.OutputStream);

2 comments:

Matt MacDonald said...
October 29, 2008 at 1:49 PM  

This is a really great algorithm. My only problem with it is that it is very slow when you are doing border widths greater than a few pixels. For instance, it takes about 7 to 15 seconds (depending on server load) to render some text with a border size of 20. This isn't a realistic timeframe, especially if multiple lines of text are required. Is there any way we can avoid the nested loops in the middle? That seems to be the bottleneck. Other than that, the text renders perfectly, much better than other examples I have seen.

Reeta said...
July 4, 2011 at 6:56 AM  

Hi mate,
This is really wonderful information I am searching for. Thanks for sharing this one.


Datastage