Translate/Rotate previously drawn rectangle, is th

2019-09-17 05:59发布

问题:

I have a bmp with text, for ease of access the bmp rotates accordingly to the user options. Problem: the text gets inverted whenever the image rotates. (only strict angles, 90, 180, etc)

Possible solution: Rotate the text at 180º then use the regular rotation, so that it doesnt get mirrored?

I tried this the following way, I create a rectangle around the text and turn it.

    Rectangle r1 = new Rectangle((int)(ecobdesenho / 10) / 2 + esp - 15, esp - 25, 25, 12);
            using (Matrix m = new Matrix())
            {

                m.RotateAt(180, new PointF((int)(ecobdesenho / 10) / 2 + esp - 15 + (25 / 2),
                                          esp - 25 + (12 / 2)));


                 g.Transform = m;
                 g.DrawRectangle(new Pen(Color.Black), r1);
                 g.DrawString("e/10", fnt2, new SolidBrush(Color.Black), (int)(ecobdesenho / 10) / 2 + esp - 15, esp - 25); //15 para tras, 15 para cima

                g.ResetTransform();
            }

Then, i provide the bmp with the rotation before its drawings:

            g.TranslateTransform((float)(xWcorrigido / 2 + esp), (float)(yWcorrigido / 2 + esp));
            g.RotateTransform(180);
            g.TranslateTransform((float)(-xWcorrigido / 2 - esp), (float)(-yWcorrigido / 2 - esp)); 

However this combination doesnt affect the previous text. I tried to place it inside the using Matrix brackets, but regardless of it, it doesnt apply the whole transformation process.

I could too, first use the general translation, and then turn the text at very specific boxes, but it would give the same amount of work i'm trying to avoid.

Any hint? My brain already hurts with so many possible combinations

回答1:

The solution is applying the transformation within the matrix, and for the matrix, like this:

        Rectangle r1 = new Rectangle((int)(ecobdesenho / 10) / 2 + esp - 15, esp - 25, 25, 12);
        using (Matrix m = new Matrix())
        {  
            m.TranslateTransform((float)(xWcorrigido / 2 + esp), (float)(yWcorrigido / 2 + esp));
            m.RotateTransform(180);
            m.TranslateTransform((float)(-xWcorrigido / 2 - esp), (float)(-yWcorrigido / 2 - esp)); 

            m.RotateAt(180, new PointF((int)(ecobdesenho / 10) / 2 + esp - 15 + (25 / 2),
                                      esp - 25 + (12 / 2)));


             g.Transform = m;
             g.DrawRectangle(new Pen(Color.Black), r1);
             g.DrawString("e/10", fnt2, new SolidBrush(Color.Black), (int)(ecobdesenho / 10) / 2 + esp - 15, esp - 25); //15 para tras, 15 para cima

            g.ResetTransform();
        }