C# DrawString with StringFormatFlags: How to Flip Vertical Orientation?

624 Views Asked by At

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

There are 0 best solutions below