Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Rotate Text - Find bounding rectangle

Rotate Text - Find bounding rectangle

Scheduled Pinned Locked Moved C#
graphicstutorialquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 643528
    wrote on last edited by
    #1

    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)??

    H C 2 Replies Last reply
    0
    • U User 643528

      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)??

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Graphics.MeasureString is probably what you want. Depending on your needs, you can resize the SizeF struct you get back. Use that to construct a RectangleF (the PointF for the original will depending on your other transform parameters) and use Graphics.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_|
      C

      Is that right?

      Software Design Engineer Developer Division Sustained Engineering, Microsoft My Articles

      W U 2 Replies Last reply
      0
      • H Heath Stewart

        Graphics.MeasureString is probably what you want. Depending on your needs, you can resize the SizeF struct you get back. Use that to construct a RectangleF (the PointF for the original will depending on your other transform parameters) and use Graphics.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_|
        C

        Is that right?

        Software Design Engineer Developer Division Sustained Engineering, Microsoft My Articles

        W Offline
        W Offline
        Werdna
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • U User 643528

          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)??

          C Offline
          C Offline
          cnurse
          wrote on last edited by
          #4

          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

          U 1 Reply Last reply
          0
          • C cnurse

            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

            U Offline
            U Offline
            User 643528
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • H Heath Stewart

              Graphics.MeasureString is probably what you want. Depending on your needs, you can resize the SizeF struct you get back. Use that to construct a RectangleF (the PointF for the original will depending on your other transform parameters) and use Graphics.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_|
              C

              Is that right?

              Software Design Engineer Developer Division Sustained Engineering, Microsoft My Articles

              U Offline
              U Offline
              User 643528
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups