在不同的旋转中绘制文本的正确方法是什么? [英] What is the correct way to draw text in different rotation?

查看:76
本文介绍了在不同的旋转中绘制文本的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个用于创建,编辑和打印标签的应用程序。在这个应用程序中,可以插入有几行文本的矩形,用户可以设置0°,90°,270°和180°之间的字体,颜色,...和方向。所以我编写了一个方法,根据方向,使用GDI +和DrawString方法在边界矩形内绘制文本。

I have developed an application to create, edit and print labels. In this application it is possible to insert rectangles in which there are several lines of text and the user can set the font, the color,.., and the orientation among 0°, 90°, 270° and 180° degrees. So I wrote a method to draw the text inside the bounding rectangle, according to the orientation, using GDI+ and DrawString method.

switch (RotationAngle)
            {
                case 90:
                    g.RotateTransform(90);
                    objRec = DrawRectangle.GetNormalizedRectangle(objRec.Top, 
                          -objRec.Left, objRec.Bottom, -objRec.Right);
                    g.DrawString(lines[i], textFont, brush, objRec, sf);
                    g.RotateTransform(-90);
                    break;
                case 180:
                    g.RotateTransform(180);
                    objRec = DrawRectangle.GetNormalizedRectangle(-objRec.Left, 
                          -objRec.Top, -objRec.Right, -objRec.Bottom); 
                    g.DrawString(lines[i], textFont, brush, objRec, sf);
                    g.RotateTransform(-180);
                    break;
                case 270:
                    g.RotateTransform(270);
                    objRec = DrawRectangle.GetNormalizedRectangle(-objRec.Top, 
                        +objRec.Left, -objRec.Bottom, +objRec.Right);
                    g.DrawString(lines[i], textFont, brush, objRec, sf);
                    g.RotateTransform(-270);
                    break;
                case 0:
                    g.DrawString(lines[i], textFont, brush, objRec, sf);
                    break;
            }





其中objRect是用户设置的原始矩形。该方法适用于所有方向,但不适用于180°!在这种情况下,文本绘制为0度。我真的不知道该怎么做,我很想找到解决问题的正确方法。在不同的旋转中绘制文本的正确方法是什么?任何帮助都会令人愉快。在此先感谢。



where "objRect" is the original rectangle set by the user. The method works for all the orientation, but not for 180°! In this case the text is drawn as at 0 degrees. I really don't know what to do and I'm going crazy in finding the correct way to solve the problem. What is the correct way to draw text in different rotation? Any help would be pleasant. Thanks in advance.

推荐答案

在面板的paint事件中快速检查代码,我们可以正常工作180但是我不能使用你的DrawRectangle。 GetNormalizedRectangle方法,因为我没有它,所以我使用了TranslateTransform:

A quick check with your code in the paint event of a panel gives we the 180 working fine - but I can't use your DrawRectangle.GetNormalizedRectangle method because I don't have it, so I used TranslateTransform instead:
g.TranslateTransform(myDrawingPanel.Width, myDrawingPanel.Height);
g.RotateTransform(180);
//objRec = DrawRectangle.GetNormalizedRectangle(-objRec.Left,
//      -objRec.Top, -objRec.Right, -objRec.Bottom);
g.DrawRectangle(Pens.Red, objRect);
g.DrawString(lines[i], textFont, brush, objRec, sf);
g.RotateTransform(-180);



工作正常 - 正如我所料,文字是颠倒的,从右到左。

我建议你想看看你的方法 - 可能是它导致了问题。它也值得一看你的StringFormat设置 - 再次我没有那些。





使用的图形在这个方法中是PrintPageEventArgs的Graphics对象;如果我选择一个pdf虚拟打印机,这个方法似乎不能工作180°。在所有其他情况下,它工作正常并产生预期的结果。仅适用于pdf打印机和方向180°,指令g.RotateTransform(-180)似乎完全被忽略了!我知道这看起来很奇怪,但我找不到解决办法!



我刚尝试过它:


That worked fine - the text was upside down and right to left, as I would expect.
I would suggest that you want to look at your method - it may be that it is causing the problem. It could also be worth looking at your StringFormat settings - again I don't have those.


"The graphics used in this method is the Graphics object of a PrintPageEventArgs; it seems that this method doesn't work for 180° just if I select a pdf virtual printer. In all other cases it works fine and produce the result expected. Only for pdf printers and orientation 180° degrees, the instruction g.RotateTransform(-180) seems to be ignored at all! I know that it seems weird, but I can't find a solution!"

I just tried it with mine:

    PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
    pd.Print();
    }

void pd_PrintPage(object sender, PrintPageEventArgs e)
    {
    Graphics g = e.Graphics;
    RectangleF objRec = new RectangleF(50, 50, 100, 75);
    g.TranslateTransform(myDrawingPanel.Width, myDrawingPanel.Height);
    g.RotateTransform(180);
    g.DrawRectangle(Pens.Red, new Rectangle(50, 50, 100, 75));
    g.DrawString("Hello", font, Brushes.Blue, new RectangleF(50, 50, 100, 75), new StringFormat());
    g.RotateTransform(-180);
    }

使用CutePDF作为打印机驱动程序并且工作正常 - 我将文本翻转为蓝色,倒置在红色框内。



您使用的是哪个PDF系统?

Using CutePDF as the printer driver and it worked fine - I get the text in blue, upside down, inside a red box.

Which PDF system are you using?


这篇关于在不同的旋转中绘制文本的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆