DrawString Method

  • Thread starter Thread starter Mark Keats
  • Start date Start date
M

Mark Keats

Hi there,

I have some code for an ASP.Net page that produces an image of an e-mail
address to prevent those programs that spider the net for e-mail
addresses on pages grabbing it.

The code is as follows (but on my version it gets the e-mail address
from a database rather than QueryString).

How can I crop the image to the exact size of the text as if the e-mail
address is short there is excess whitespace and if it is long it
over-runs the image.

Any help would be great!
// Set Content Type to GIF
Response.ContentType = "image/gif";

// Create Image
const int width = 250, height = 20;
Bitmap objBitmap = new Bitmap(width, height);
Graphics objGraphics = Graphics.FromImage(objBitmap);
objGraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);

// Create Font and String Format
Font fontDesc = new Font("Verdana", 8, FontStyle.Underline);
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Near;
stringFormat.LineAlignment = StringAlignment.Center;

// Draw the String
objGraphics.DrawString(Request.QueryString["email"], fontDesc, new SolidBrush(Color.Blue), new Rectangle(0, 0, width, height), stringFormat);

// Save the Image to the OutputStream
objBitmap.Save(Response.OutputStream, ImageFormat.Gif);

// Clean Up
objGraphics.Dispose();

--

Mark (Sparky) Keats
+-+-+-+-+-+-+-+-+-+

e: (e-mail address removed)
web: http://www.sparky4646.co.uk

---------------------------------------------------------------------

"Computer games don't affect kids, I mean if Pac Man affected us
as kids, we'd all run around in a darkened room munching pills
and listening to repetitive music..."

- Kristian Wilson, CEO, Nintendo Gaming Corporation, Inc, 1989

---------------------------------------------------------------------
 
Why don't you use MeasureString first to find out the dimensions
necessary for drawing the string in the specified font. Then create
your bitmap to the size returned plus x pixels.

Jonathan Schafer


Hi there,

I have some code for an ASP.Net page that produces an image of an e-mail
address to prevent those programs that spider the net for e-mail
addresses on pages grabbing it.

The code is as follows (but on my version it gets the e-mail address
from a database rather than QueryString).

How can I crop the image to the exact size of the text as if the e-mail
address is short there is excess whitespace and if it is long it
over-runs the image.

Any help would be great!
// Set Content Type to GIF
Response.ContentType = "image/gif";

// Create Image
const int width = 250, height = 20;
Bitmap objBitmap = new Bitmap(width, height);
Graphics objGraphics = Graphics.FromImage(objBitmap);
objGraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);

// Create Font and String Format
Font fontDesc = new Font("Verdana", 8, FontStyle.Underline);
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Near;
stringFormat.LineAlignment = StringAlignment.Center;

// Draw the String
objGraphics.DrawString(Request.QueryString["email"], fontDesc, new SolidBrush(Color.Blue), new Rectangle(0, 0, width, height), stringFormat);

// Save the Image to the OutputStream
objBitmap.Save(Response.OutputStream, ImageFormat.Gif);

// Clean Up
objGraphics.Dispose();
 
Back
Top