stine.grith
1/20/2005 9:20:00 AM
We are generating a serverside gif-image from a textinput. The problem
is that we also are trying to draw a border around the text at the
image.
First we draw the image using Bitmap and graphics, then we use
GraphicsPath and Pen to draw the text, which is white with a black
border.
The problem is that the border is "following" the letter at the inside
and at the outside, which results in peaks at some letters, like A and
W.
The code we use is:
Response.ContentType="image/gif";
string field=Request.QueryString["text"];
string height = Request.QueryString["Size"];
Color myColor = ColorTranslator.FromHtml("#" +
Request.QueryString["BgColor"]);
if (field!=null)
{
Bitmap bm=new Bitmap(1,1);
int h = (int) Convert.ToInt32(height);
Font font = new Font("SteileFuturaBQ",h,FontStyle.Italic);
int fontStyle = (int)FontStyle.Italic;
StringFormat sf=new StringFormat(StringFormatFlags.NoWrap);
SizeF stringsize=Graphics.FromImage(bm).MeasureString(field, font);
bm=new Bitmap((int)(stringsize.Width), (int)(stringsize.Height));
System.Diagnostics.Debug.WriteLine(bm.Height);
System.Diagnostics.Debug.WriteLine(bm.Width);
Graphics gr=Graphics.FromImage(bm);
gr.FillRegion(Brushes.LightYellow, new Region(new RectangleF(0, 0,
bm.Width, bm.Height)));
gr.DrawRectangle(new Pen(Color.Black, 1), 0, 0, bm.Width-1,
bm.Height-1);
gr.SmoothingMode = SmoothingMode.AntiAlias;
GraphicsPath pth = new GraphicsPath();
pth.AddString(field.Trim(), font.FontFamily ,fontStyle, h ,new
Point(0,0), StringFormat.GenericTypographic);
Pen p = new Pen(Brushes.Black, 3.0f);
gr.DrawPath(p,pth);
gr.FillPath(Brushes.White,pth);
System.Diagnostics.Debug.WriteLine(bm.Height);
System.Diagnostics.Debug.WriteLine(bm.Width);
p.Dispose();
pth.Dispose();
bm.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
Response.Flush();
Response.End();