Smith
Smith

Reputation: 5961

Display text properly within ellipse

I need to properly display text within a drawn ellipse in c#, currently here is what i am using.

e.Graphics.DrawString(this.Text, base.Font, new SolidBrush(_NormalColorA), this.ClientRectangle, GetStringFormat(this.TextAlign));  

The GetStringFormat function

static internal StringFormat GetStringFormat(ContentAlignment ctrlalign)
        {
            StringFormat strFormat = new StringFormat();
            switch (ctrlalign)
            {
                case ContentAlignment.MiddleCenter:
                    strFormat.LineAlignment = StringAlignment.Center;
                    strFormat.Alignment = StringAlignment.Center;
                    break;
                case ContentAlignment.MiddleLeft:
                    strFormat.LineAlignment = StringAlignment.Center;
                    strFormat.Alignment = StringAlignment.Near;
                    break;
                case ContentAlignment.MiddleRight:
                    strFormat.LineAlignment = StringAlignment.Center;
                    strFormat.Alignment = StringAlignment.Far;
                    break;
                case ContentAlignment.TopCenter:
                    strFormat.LineAlignment = StringAlignment.Near;
                    strFormat.Alignment = StringAlignment.Center;
                    break;
                case ContentAlignment.TopLeft:
                    strFormat.LineAlignment = StringAlignment.Near;
                    strFormat.Alignment = StringAlignment.Near;
                    break;
                case ContentAlignment.TopRight:
                    strFormat.LineAlignment = StringAlignment.Near;
                    strFormat.Alignment = StringAlignment.Far;
                    break;
                case ContentAlignment.BottomCenter:
                    strFormat.LineAlignment = StringAlignment.Far;
                    strFormat.Alignment = StringAlignment.Center;
                    break;
                case ContentAlignment.BottomLeft:
                    strFormat.LineAlignment = StringAlignment.Far;
                    strFormat.Alignment = StringAlignment.Near;
                    break;
                case ContentAlignment.BottomRight:
                    strFormat.LineAlignment = StringAlignment.Far;
                    strFormat.Alignment = StringAlignment.Far;
                    break;
            }

            strFormat.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.None;

            return strFormat;
        }

In the picture bellow, you can see the problem and the expected, any ideas?

enter image description here

Upvotes: 0

Views: 198

Answers (1)

RogerN
RogerN

Reputation: 3821

Since an ellipse is geometrically equivalent to a circle that has been scaled linearly, and since your major and minor axes are always aligned with the X and Y axes, the inner rectangle which maximizes the available area for your text can be found by simply multiplying the total width and height of your ellipse by sqrt(2)/2.

Given a rectangle describing your ellipse, this function ought to calculate the correct layout rectangle for your text:

private static RectangleF GetEllipseInnerRect(RectangleF ellipse) {
    const float HalfSqrt2 = 0.707107f;
    var innerSize = new SizeF(ellipse.Width * HalfSqrt2, ellipse.Height * HalfSqrt2);
    return new RectangleF(
        ellipse.Left + 0.5f * (ellipse.Width - innerSize.Width),
        ellipse.Top + 0.5f * (ellipse.Height - innerSize.Height),
        innerSize.Width,
        innerSize.Height);
}

Upvotes: 1

Related Questions