# Coordinates with StringAlignment RSS

## 3 replies

Last post Oct 19, 2006 09:35 PM by jeepzj

None

0 Points

43 Posts

### Coordinates with StringAlignment

Oct 18, 2006 09:26 PM|skuggbo|LINK

Hi,

I'm using a RectangleF object that is as large as my image. I will write a text with a DrawString method. Where the text will be placed depends on the StringFormat.Alignment.

My question is, If I set the alignment to, i.e. center how can I find the correct coordinates of the string. Alignment.Center is almost the same as (Image Width / 2) - (String Width / 2), but I need the same coordinates as DrawString uses to draw, how can I find it?

Regards,

Robert

None

0 Points

49 Posts

### Re: Coordinates with StringAlignment

Oct 18, 2006 09:55 PM|jeepzj|LINK

You can use Graphics.MeasureString method to measure the String's Size, this method can measure the whole string Width and Height.
http://www.photosprite.net

None

0 Points

43 Posts

### Re: Coordinates with StringAlignment

Oct 19, 2006 12:59 PM|skuggbo|LINK

Yes I know that, thats how I get the string width.

The problem is that I need to know exactly the coordinates of the StringAlignment.Center position is.

Is it possible to get the functionality of i.e. Drawstring and see how it use StringFormat to position the string?

None

0 Points

49 Posts

### Re: Coordinates with StringAlignment

Oct 19, 2006 09:35 PM|jeepzj|LINK

Below is the pseudocode that implement your function.

// Get the image's size.
Size imageSize = new Size(image.Width, image.Height);

// Measure the string's whole size.
Size fontSize = Graphics.MeasureString();

// Calculate  the top-left point of the string so that locate the string exactly.
Point position = new Point();
position.X = (imageSize.Width - fontSize.Width) / 2;
position.Y = (imageSize.Height - fontSize.Height) / 2;

// Use DrawString method to draw the string on the image.
Graphics.DrawString(yourText, position.X, position.Y);

MeasureString

http://www.photosprite.net