Translate/Rotate previously drawn rectangle, is this possible?

穿精又带淫゛_ 提交于 2019-12-13 06:37:20

问题


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();
        }


来源:https://stackoverflow.com/questions/28803156/translate-rotate-previously-drawn-rectangle-is-this-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!