1

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();
    }
  • 1
    https://stackoverflow.com/questions/4421381/how-to-rotate-text-in-gdi/4421398#4421398 – dr.null Apr 08 '22 at 18:50
  • That is close but I'm placing text that has to be centered which uses a rect. The rect is already positioned. That code puts my text way out in wonky places instead of where the rect is. I've added more code. – AlwaysStuck Apr 11 '22 at 16:54
  • Draw the string in a Rectangle: `backgroundGfx.DrawString(textFeature.Text, font, textBrush, backgroundGfx.VisibleClipBounds, stringFormat);` otherwise the `StringFormat` will do nothing. Center the alignment properties: `stringFormat.Alignment = stringFormat.LineAlignment = StringAlignment.Center;`. Dispose the graphics objects when you finish or create them with `using` keyword. You just need the last four lines to rotate and draw the string. – dr.null Apr 11 '22 at 22:00
  • @dr.null Sorry for the long delay. Had other priorities. This suggestion got me closer, but it is drawing the text rotated in the middle of the image instead of in the X and Y position I've assigned. Do I need to move the transform of backgroundGfx or am I missing a step? The only change to the above code is replacing the DrawString with your suggestion. – AlwaysStuck Apr 25 '22 at 17:23
  • 1
    I got it working. I created a new image just to draw the text on and used that in place of the backgroundGfx, and then added it to backgroundGfx using DrawImage. Worked great! Thanks so much for your help! – AlwaysStuck Apr 25 '22 at 18:23

0 Answers0