Monday, August 10, 2009

Outline Text or Blur on Text Image using GDI+

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

No comments:

Post a Comment