Rotate Text - Find bounding rectangle
-
I am rotating text via the Graphics.RotateTransformation technique. After drawing my text string I set the rotation transform back to normal. I then want to draw a "non-rotated" rectangle around the rotated text. Does anyone know how to calculate this rectangle (the largest non-rotated rectangle around the rotated text)??
-
I am rotating text via the Graphics.RotateTransformation technique. After drawing my text string I set the rotation transform back to normal. I then want to draw a "non-rotated" rectangle around the rotated text. Does anyone know how to calculate this rectangle (the largest non-rotated rectangle around the rotated text)??
Graphics.MeasureString
is probably what you want. Depending on your needs, you can resize theSizeF
struct you get back. Use that to construct aRectangleF
(thePointF
for the original will depending on your other transform parameters) and useGraphics.DrawRectangle
to draw it. If you need additional help, please be a little more specific. From what I gather, you want the final surface to display something like this (ASCII art):_A_
|_B_|
CIs that right?
Software Design Engineer Developer Division Sustained Engineering, Microsoft My Articles
-
Graphics.MeasureString
is probably what you want. Depending on your needs, you can resize theSizeF
struct you get back. Use that to construct aRectangleF
(thePointF
for the original will depending on your other transform parameters) and useGraphics.DrawRectangle
to draw it. If you need additional help, please be a little more specific. From what I gather, you want the final surface to display something like this (ASCII art):_A_
|_B_|
CIs that right?
Software Design Engineer Developer Division Sustained Engineering, Microsoft My Articles
You can use a trick and use Matrix class to rotate points. For example if you're drawing text within a rectangle (before rotation) Rectangle rect = ... get draw rectangle ... ... rotate and draw text... Matrix m = new Matrix(); m.Rotate(angle) or m.RotateAt(m) Point[] points = new Point[4]; points[0] = new Point(rect.X, rect.Y) points[1] ... get all 4 corners of the rectangle for (int i=0; i
-
I am rotating text via the Graphics.RotateTransformation technique. After drawing my text string I set the rotation transform back to normal. I then want to draw a "non-rotated" rectangle around the rotated text. Does anyone know how to calculate this rectangle (the largest non-rotated rectangle around the rotated text)??
-
Please could you post a full snippet of how to do rotated text as you describe, as that would be useful to know, from my point of view. In olrder apps I have been using LOGFONT stuff and API calls. Nursey
To rotate the text use the power of the Graphics class in .net. It has a RotateTransform() method. Here is one link that talks about how to use it. I'm sure you can find other tutorials on the web also. http://www.c-sharpcorner.com/Code/2004/April/Transformations06.asp
-
Graphics.MeasureString
is probably what you want. Depending on your needs, you can resize theSizeF
struct you get back. Use that to construct aRectangleF
(thePointF
for the original will depending on your other transform parameters) and useGraphics.DrawRectangle
to draw it. If you need additional help, please be a little more specific. From what I gather, you want the final surface to display something like this (ASCII art):_A_
|_B_|
CIs that right?
Software Design Engineer Developer Division Sustained Engineering, Microsoft My Articles
Actually what I needed is a bit more complicated. I first used MeasureString to create a rectangle for the text. Then I want to rotate that rectangle and find the largest "non-rotate" rectangle that rectangle will fit in. I don't care about the location of the largest rectangle, just the width and height. I did figure this out with a bunch of math. here is the routine if anyone is interested. Note that the rect that is passed into this routine is the rect created using MeasureString. private RectangleF FindBoundingRect(RectangleF rect, float angle) { // Do a quick short cut here if the text is not really rotated // but is instead horizontal or rotated. if (angle == 0 || angle == 180 || angle == 360) { return new RectangleF(0, 0, rect.Width, rect.Height); } else if (angle == 90 || angle == 270) { return new RectangleF(0, 0, rect.Height, rect.Width); } else if (angle > 360) { // Don't handle this case for now. return new RectangleF(0, 0, 0, 0); } // First transform the angle to radians. float radianAngle = angle * (float)(Math.PI / 180.0F); float radian90Degrees = 90 * (float)(Math.PI / 180.0F); // First find the rotated point 1. This is the bottom right point. The radius // for this point is the width. float x = (float)(rect.Width * Math.Cos(radianAngle)); float y = (float)(rect.Width * Math.Sin(radianAngle)); PointF p1 = new PointF(x, y); // Now find point 3 which is also easy. This is the top left point. The // radius for this point is the height. x = (float)(rect.Height * Math.Cos(radianAngle + radian90Degrees)); y = (float)(rect.Height * Math.Sin(radianAngle + radian90Degrees)); PointF p3 = new PointF(x, y); // Point 2, the top right point, is a bit trickier. First find the angle // from the bottom left up to the top left when the rect is not rotated. float radianAngleP2 = (float) Math.Atan(rect.Height / rect.Width); // Now find the length of the diagonal line between these two points. float radiusP2 = (float) Math.Sqrt((rect.Height * rect.Height) + (rect.Width * rect.Width)); // Now we can find the x,y of point 2 rotated by the specified angle. x = (float)(radiusP2 * Math.Cos(radianAngle + radianAngleP2)); y = (float)(radiusP2 * Math.Sin(radianAngle + radianAngleP2)); PointF p2 = new PointF(x, y); // Now based on the original angle we can figure out the width and height // of our returned rectagle. float width = 0; float height = 0; if (angle < 90) { width = p1.X + Math.Ab