Possible alignment bug in DrawString() ??

D

David Lindgren

Hello!

I am using the DrawString method with different StringAlignments passed to
it and the result varies alot!

Take a look at this screenshot:
http://www.lowrad.net/files/alignment_screenshot.jpg

As you can see I am printing four different text in each of the the parts of
this image. ( The three parts are three images)
The first two lines in each part is printed with StringAlignment.Center and
the two texts in the last line is printed with StringAlignment.Left and
StringAlignment.Right.

If you're zooming the screenshot you can see that none of the texts that's
supposed to be centered is and the text that should be right aligned in the
part to the right is even printed outside it's rect.

This must be a bug in GDI+ ?
Is there a fix for it?

This is my code:

PaintBaseFigure(bmpBRUB,"Balansräkning","Period 199401-199412","UB
Tillgångar","UB Skulder");
PaintBaseFigure(bmpBRIB,"Balansräkning","Period 199401-199412","IB
Tillgångar","IB Skulder");
PaintBaseFigure(bmpRR,"Resultaträkning","Period
199401-199412","Kostnader","Intäkter");

private void PaintBaseFigure(Bitmap bmp, string strCaption, string
strPeriod, string strLeftText, string strRightText)
{
Graphics g = Graphics.FromImage(bmp);

// Rita ut linjerna...
int totalBarHeight = 350;
int horLineLength = 200;
int vertLineLength = totalBarHeight + 30;
float LineThickness = 5.0F;
Point horLineStartPoint = new Point(15,60);
Point vertLineStartPoint = new Point((horLineStartPoint.X +
horLineLength/2),horLineStartPoint.Y);

Brush blackBrush = Brushes.Black;
Pen pen = new Pen(blackBrush,LineThickness);

g.DrawLine(pen,horLineStartPoint,new Point(horLineStartPoint.X +
horLineLength, horLineStartPoint.Y));
g.DrawLine(pen,vertLineStartPoint,new Point(vertLineStartPoint.X,
vertLineStartPoint.Y + vertLineLength));

// Skriv ut texterna...
StringFormat stringformat = new StringFormat();
stringformat.LineAlignment = StringAlignment.Center;
stringformat.Alignment = StringAlignment.Center;
Brush stringBrush = Brushes.Black;
int LineSpacing = 0;

Font captionFont = new Font("Arial",14.0F,FontStyle.Bold);
RectangleF captionRect = new RectangleF(new
PointF(horLineStartPoint.X,0.0F),new SizeF(horLineLength,captionFont.Height
+ LineSpacing));
g.FillRectangle(Brushes.LightCyan,captionRect);
g.DrawString(strCaption,captionFont,stringBrush,captionRect,stringformat);

Font periodFont = new Font("Arial",10.0F);
RectangleF periodRect = new RectangleF(new
PointF(horLineStartPoint.X,captionFont.Height),new
SizeF(horLineLength,periodFont.Height + LineSpacing));
g.FillRectangle(Brushes.LightGreen,periodRect);
g.DrawString(strPeriod,periodFont,stringBrush,periodRect,stringformat);

Font leftrightFont = new Font("Arial",9.0F);

RectangleF leftRect = new RectangleF(new
PointF(horLineStartPoint.X,captionFont.Height+periodFont.Height),new
SizeF(horLineLength/2,leftrightFont.Height + LineSpacing));
stringformat.Alignment = StringAlignment.Near;
g.FillRectangle(Brushes.LightPink,leftRect);
g.DrawString(strLeftText,leftrightFont,stringBrush,leftRect,stringformat);

RectangleF rightRect = new RectangleF(new
PointF(horLineStartPoint.X+horLineLength/2,captionFont.Height+periodFont.Hei
ght),new SizeF(horLineLength/2,leftrightFont.Height + LineSpacing));
stringformat.Alignment = StringAlignment.Far;
g.FillRectangle(Brushes.LightSalmon,rightRect);

g.DrawString(strRightText,leftrightFont,stringBrush,rightRect,stringformat);
}


Thanks for your help!

/David.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top