473,473 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

DrawString Method

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: ma**@sparky4646.co.uk
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

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

Nov 15 '05 #1
2 14857
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
On Fri, 01 Aug 2003 16:39:30 +0100, Mark Keats <ma**@sparky4646.co.uk>
wrote:
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();


Nov 15 '05 #2
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
On Fri, 01 Aug 2003 16:39:30 +0100, Mark Keats <ma**@sparky4646.co.uk>
wrote:
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();


Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lex | last post by:
I have a c# app that has a OwnerDrawn menu items. Is there and easy way to use DrawString to draw the underline under the mnemonic character? BTW - I figured out how to detected if we should...
1
by: DrDevious | last post by:
Maybe I am doing something wrong but has anyone else here noticed a difference in the positioning of text between the Graphics.DrawString method and the Win32 GDI DrawText function? My text is...
1
by: David Lindgren | last post by:
Hello! I am using the DrawString method with different StringAlignments passed to it and the result varies alot! Take a look at this screenshot:...
1
by: Drew | last post by:
I posted this to framework.drawing, but did not get any responses... Maybe here?? I am drawing some text using DrawString() and using a bounding rectangle so that when the string is too long it...
3
by: Rich | last post by:
Hello, I need to draw some text on a form. No problem .... e.Graphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat) My problem is that I want to draw the text on top of some...
1
by: Joao | last post by:
Hi, I'm trully excited to use VB.NET's PrintDocument to make my printouts. On them I draw strings, images and lines. All perfect, if I invoke the Print or the PrintPreview one time per run. At...
2
by: Gary | last post by:
I need to put a bunch of characters in a grid like control, and am using DrawString method to put the character. The problem is: 1. The size of drawing rect is fixed, 32x32 2. For some fonts, 14...
5
by: johnb41 | last post by:
I need to print out a string of text and obviously i'm using the DrawString command. But the string must be placed AFTER some "programmatically generated text" (also printed using DrawString). ...
4
by: nothix9 | last post by:
Hello guys, Ive been working this for 2 days, I am required to position a string using drawstring but the problem is its dynamic and changes depending on the database or source. A quick fix I thought...
2
by: ChrisNightingale | last post by:
Hi everybody, I have an odd issue which I'm not sure how to resolve. I'm basically implementing a print mechanism which takes a series of controls and reproduces them on a print document. So...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.