Last post Oct 19, 2006 09:35 PM by jeepzj
Oct 18, 2006 09:26 PM|skuggbo|LINK
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?
Oct 18, 2006 09:55 PM|jeepzj|LINK
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?
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);