Monday, December 1, 2008

DRAW STRING IN FIX RECTANGLE WITH BACKGROUNG COLOR, BORDER AND SHADOW

·

using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;



// CREATE ENUM FOR SHADOW STYLE
enum ShadowStyle
{
Top_Left,
Top_Right,
Bottom_Left,
Bottom_Right,
None
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Color fontColor = Color.Red;
Color shadowColor = Color.Blue; // SET SHADOW COLOR
Color borderColor = Color.Yellow;
Color bg_Color = Color.Black;

bool fontBold = false;
bool fontItalic = false;
bool fontUnderline = false;

string text = "WELCOME";
string fontFamily = "impact";
int w = 600;
int h = 200;
int borderWidth = 5;

ShadowStyle shd = ShadowStyle.Top_Right; // SET SHADOW STYLE

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

Rectangle target = new Rectangle(0, 0, w, h);
Bitmap bitmap = new Bitmap(w, h);
Graphics g = Graphics.FromImage(bitmap);

color = new SolidBrush(bg_Color);
g.FillRectangle(color, target);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;

int trgheight = target.Height;
int x = 3, y = 3;
int tLeft = target.Left + x;
int tTop = target.Top + x;
int tRight = target.Right - x;
int tBottom = target.Bottom - x;


Font the_font = new Font(fontFamily, trgheight, fontStyle, GraphicsUnit.Pixel);

StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;


GraphicsPath text_path = new GraphicsPath();


/* Shadow Style */


if (shd != ShadowStyle.None)
{
text_path.AddString(text, the_font.FontFamily, (int)fontStyle, trgheight, new PointF(0, 0), sf);
RectangleF text_rectf1 = text_path.GetBounds();
PointF[] target_pts1 = new PointF[3];
switch (shd)
{
case ShadowStyle.Top_Right:
target_pts1[0] = new PointF(tLeft + y, tTop - y);
target_pts1[1] = new PointF(tRight + y, tTop - y);
target_pts1[2] = new PointF(tLeft + y, tBottom - y);
break;
case ShadowStyle.Top_Left:
target_pts1[0] = new PointF(tLeft - y, tTop - y);
target_pts1[1] = new PointF(tRight - y, tTop - y);
target_pts1[2] = new PointF(tLeft - y, tBottom - y);
break;
case ShadowStyle.Bottom_Right:
target_pts1[0] = new PointF(tLeft + y, tTop + y);
target_pts1[1] = new PointF(tRight + y, tTop + y);
target_pts1[2] = new PointF(tLeft + y, tBottom + y);
break;
case ShadowStyle.Bottom_Left:
target_pts1[0] = new PointF(tLeft - y, tTop + y);
target_pts1[1] = new PointF(tRight - y, tTop + y);
target_pts1[2] = new PointF(tLeft - y, tBottom + y);
break;
default:
target_pts1[0] = new PointF(tLeft, tTop);
target_pts1[1] = new PointF(tRight, tTop);
target_pts1[2] = new PointF(tLeft, tBottom);
break;
}
color = new SolidBrush(shadowColor);
g.Transform = new Matrix(text_rectf1, target_pts1);
g.FillPath(color, text_path);
text_path = new GraphicsPath();
}

/* Draw Text */

text_path.AddString(text, the_font.FontFamily, (int)fontStyle, trgheight, new PointF(0, 0), sf);
RectangleF text_rectf = text_path.GetBounds();

PointF[] target_pts = new PointF[3];
switch (shd)
{
case ShadowStyle.Top_Right:
target_pts[0] = new PointF(tLeft, tTop + y);
target_pts[1] = new PointF(tRight - y, tTop + y);
target_pts[2] = new PointF(tLeft, tBottom);
break;
case ShadowStyle.Top_Left:
target_pts[0] = new PointF(tLeft + y, tTop + y);
target_pts[1] = new PointF(tRight + y, tTop + y);
target_pts[2] = new PointF(tLeft + y, tBottom);
break;
case ShadowStyle.Bottom_Right:
target_pts[0] = new PointF(tLeft, tTop - y);
target_pts[1] = new PointF(tRight - y, tTop - y);
target_pts[2] = new PointF(tLeft, tBottom);
break;
case ShadowStyle.Bottom_Left:
target_pts[0] = new PointF(tLeft + y, tTop - y);
target_pts[1] = new PointF(tRight + y, tTop - y);
target_pts[2] = new PointF(tLeft + y, tBottom);
break;
default:
target_pts[0] = new PointF(tLeft, tTop);
target_pts[1] = new PointF(tRight, tTop);
target_pts[2] = new PointF(tLeft, tBottom);
break;
}
g.Transform = new Matrix(text_rectf, target_pts);

color = new SolidBrush(fontColor);
g.FillPath(color, text_path);

/* DRAW BORDER */
color = new SolidBrush(borderColor);
Pen pen = new Pen(color);
for (int i = 0; i <= borderWidth; i++)
{
for (int j = 0; j <= borderWidth; j++)
{
text_path.AddString(text, the_font.FontFamily, (int)fontStyle, target.Height, new PointF(i, j), sf);
}
}
g.DrawPath(pen, text_path);
/* END */

g.ResetTransform();
text_path.Dispose();
sf.Dispose();
the_font.Dispose();
g.Dispose();
bitmap.Save(Server.MapPath("TextBorderShadow.png"), System.Drawing.Imaging.ImageFormat.Png);

}
}

0 comments: