用StringFormatFlags的C#牵引线:如何翻转垂直方向?

发布于 2025-01-20 01:35:40 字数 2250 浏览 0 评论 0原文

我正在在图像上绘制字符串,并将其保存为TIFF。我需要以这样的方式定义文本:

所需的输出

我正在使用此代码来创建此代码字符串格式:

formatFlags = (StringFormatFlags.NoClip | StringFormatFlags.DirectionVertical | StringFormatFlags.DirectionRightToLeft);

这是输出:

实际输出

我如何简单地将方向翻转?

更新:根据评论,我尝试了此操作,但是它是将文本置于角落的位置,而不是如果我不添加该示例代码,则将其放置在角落。我评论了我的原始束带。

    protected virtual void AddTextToBackground(Graphics backgroundGfx, FeatureLocation featureLocation, TextFeature textFeature, int equator) {
        Font font = CreateFont(textFeature);
        Color fontColor = ColorTranslator.FromHtml(textFeature.FontColor);
        Brush textBrush = new SolidBrush(fontColor);

        // Determine postion of text box
        int xPos = featureLocation.XPos - TEXT_RECT_WIDTH / 2;

        int adjustedEquatorOffset = CalculateTextEquatorOffset(featureLocation.EquatorOffset);
        //int adjustedEquatorOffset = featureLocation.EquatorOffset;

        int yPos = CalculateYPos(equator, adjustedEquatorOffset, TEXT_RECT_WIDTH);

        // Rectangle is necessary to create centered text using StringFormat in the DrawString call
        Rectangle rect = new Rectangle(xPos, yPos, TEXT_RECT_WIDTH, TEXT_RECT_WIDTH);

        // Set up alignment
        StringFormat stringFormat = new StringFormat {
            Alignment = StringAlignment.Center,
            LineAlignment = FindStringAlignment(featureLocation.EquatorOffset)
        };

        // Determine rotation and set format flags
        stringFormat.FormatFlags = GetTextFormatFlags(featureLocation.DefaultRotation, textFeature.Rotation);

        // Draw text
        SizeF sz = backgroundGfx.VisibleClipBounds.Size;
        backgroundGfx.TranslateTransform(sz.Width / 2, sz.Height / 2);
        backgroundGfx.RotateTransform(45);

        //backgroundGfx.DrawString(textFeature.Text, font, textBrush, rect, stringFormat);
        backgroundGfx.DrawString(textFeature.Text, font, textBrush, -(sz.Width/2), -(sz.Height/2), stringFormat);

        backgroundGfx.ResetTransform();
    }

I'm drawing strings on an image and saving it as a TIFF. I need to orient the text like this:

desired output

I'm using this code to create the string format:

formatFlags = (StringFormatFlags.NoClip | StringFormatFlags.DirectionVertical | StringFormatFlags.DirectionRightToLeft);

And this is the output:

actual output

How can I simply flip the orientation around?

Update: Based on the comment, I tried this, but it is place my text way up in the corner instead of where it would be if I didn't add in that sample code. I commented out my original DrawString.

    protected virtual void AddTextToBackground(Graphics backgroundGfx, FeatureLocation featureLocation, TextFeature textFeature, int equator) {
        Font font = CreateFont(textFeature);
        Color fontColor = ColorTranslator.FromHtml(textFeature.FontColor);
        Brush textBrush = new SolidBrush(fontColor);

        // Determine postion of text box
        int xPos = featureLocation.XPos - TEXT_RECT_WIDTH / 2;

        int adjustedEquatorOffset = CalculateTextEquatorOffset(featureLocation.EquatorOffset);
        //int adjustedEquatorOffset = featureLocation.EquatorOffset;

        int yPos = CalculateYPos(equator, adjustedEquatorOffset, TEXT_RECT_WIDTH);

        // Rectangle is necessary to create centered text using StringFormat in the DrawString call
        Rectangle rect = new Rectangle(xPos, yPos, TEXT_RECT_WIDTH, TEXT_RECT_WIDTH);

        // Set up alignment
        StringFormat stringFormat = new StringFormat {
            Alignment = StringAlignment.Center,
            LineAlignment = FindStringAlignment(featureLocation.EquatorOffset)
        };

        // Determine rotation and set format flags
        stringFormat.FormatFlags = GetTextFormatFlags(featureLocation.DefaultRotation, textFeature.Rotation);

        // Draw text
        SizeF sz = backgroundGfx.VisibleClipBounds.Size;
        backgroundGfx.TranslateTransform(sz.Width / 2, sz.Height / 2);
        backgroundGfx.RotateTransform(45);

        //backgroundGfx.DrawString(textFeature.Text, font, textBrush, rect, stringFormat);
        backgroundGfx.DrawString(textFeature.Text, font, textBrush, -(sz.Width/2), -(sz.Height/2), stringFormat);

        backgroundGfx.ResetTransform();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文