Possible alignment bug in DrawString() ??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Lindgren

    Possible alignment bug in DrawString() ??

    Hello!

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

    Take a look at this screenshot:


    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,"Balan sräkning","Peri od 199401-199412","UB
    Tillgångar","UB Skulder");
    PaintBaseFigure (bmpBRIB,"Balan sräkning","Peri od 199401-199412","IB
    Tillgångar","IB Skulder");
    PaintBaseFigure (bmpRR,"Resulta träkning","Peri od
    199401-199412","Kostna der","Intäkter" );

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

    // Rita ut linjerna...
    int totalBarHeight = 350;
    int horLineLength = 200;
    int vertLineLength = totalBarHeight + 30;
    float LineThickness = 5.0F;
    Point horLineStartPoi nt = new Point(15,60);
    Point vertLineStartPo int = new Point((horLineS tartPoint.X +
    horLineLength/2),horLineStart Point.Y);

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

    g.DrawLine(pen, horLineStartPoi nt,new Point(horLineSt artPoint.X +
    horLineLength, horLineStartPoi nt.Y));
    g.DrawLine(pen, vertLineStartPo int,new Point(vertLineS tartPoint.X,
    vertLineStartPo int.Y + vertLineLength) );

    // Skriv ut texterna...
    StringFormat stringformat = new StringFormat();
    stringformat.Li neAlignment = StringAlignment .Center;
    stringformat.Al ignment = StringAlignment .Center;
    Brush stringBrush = Brushes.Black;
    int LineSpacing = 0;

    Font captionFont = new Font("Arial",14 .0F,FontStyle.B old);
    RectangleF captionRect = new RectangleF(new
    PointF(horLineS tartPoint.X,0.0 F),new SizeF(horLineLe ngth,captionFon t.Height
    + LineSpacing));
    g.FillRectangle (Brushes.LightC yan,captionRect );
    g.DrawString(st rCaption,captio nFont,stringBru sh,captionRect, stringformat);

    Font periodFont = new Font("Arial",10 .0F);
    RectangleF periodRect = new RectangleF(new
    PointF(horLineS tartPoint.X,cap tionFont.Height ),new
    SizeF(horLineLe ngth,periodFont .Height + LineSpacing));
    g.FillRectangle (Brushes.LightG reen,periodRect );
    g.DrawString(st rPeriod,periodF ont,stringBrush ,periodRect,str ingformat);

    Font leftrightFont = new Font("Arial",9. 0F);

    RectangleF leftRect = new RectangleF(new
    PointF(horLineS tartPoint.X,cap tionFont.Height +periodFont.Hei ght),new
    SizeF(horLineLe ngth/2,leftrightFont .Height + LineSpacing));
    stringformat.Al ignment = StringAlignment .Near;
    g.FillRectangle (Brushes.LightP ink,leftRect);
    g.DrawString(st rLeftText,leftr ightFont,string Brush,leftRect, stringformat);

    RectangleF rightRect = new RectangleF(new
    PointF(horLineS tartPoint.X+hor LineLength/2,captionFont.H eight+periodFon t.Hei
    ght),new SizeF(horLineLe ngth/2,leftrightFont .Height + LineSpacing));
    stringformat.Al ignment = StringAlignment .Far;
    g.FillRectangle (Brushes.LightS almon,rightRect );

    g.DrawString(st rRightText,left rightFont,strin gBrush,rightRec t,stringformat) ;
    }


    Thanks for your help!

    /David.


  • David Lindgren

    #2
    Re: Possible alignment bug in DrawString() ?? (www.lowrad.net is down)

    It seems www.lowrad.net is down for some time (I don't know how long).
    Therefore the screenshot is also available here:


    /David.

    "David Lindgren" <david.lindgren @hogia.se.REMOV E!> skrev i meddelandet
    news:uCjYQEYmDH A.2012@TK2MSFTN GP12.phx.gbl...[color=blue]
    > Hello!
    >
    > I am using the DrawString method with different StringAlignment s 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[/color]
    of[color=blue]
    > this image. ( The three parts are three images)
    > The first two lines in each part is printed with StringAlignment .Center[/color]
    and[color=blue]
    > 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[/color]
    the[color=blue]
    > 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,"Balan sräkning","Peri od 199401-199412","UB
    > Tillgångar","UB Skulder");
    > PaintBaseFigure (bmpBRIB,"Balan sräkning","Peri od 199401-199412","IB
    > Tillgångar","IB Skulder");
    > PaintBaseFigure (bmpRR,"Resulta träkning","Peri od
    > 199401-199412","Kostna der","Intäkter" );
    >
    > private void PaintBaseFigure (Bitmap bmp, string strCaption, string
    > strPeriod, string strLeftText, string strRightText)
    > {
    > Graphics g = Graphics.FromIm age(bmp);
    >
    > // Rita ut linjerna...
    > int totalBarHeight = 350;
    > int horLineLength = 200;
    > int vertLineLength = totalBarHeight + 30;
    > float LineThickness = 5.0F;
    > Point horLineStartPoi nt = new Point(15,60);
    > Point vertLineStartPo int = new Point((horLineS tartPoint.X +
    > horLineLength/2),horLineStart Point.Y);
    >
    > Brush blackBrush = Brushes.Black;
    > Pen pen = new Pen(blackBrush, LineThickness);
    >
    > g.DrawLine(pen, horLineStartPoi nt,new Point(horLineSt artPoint.X +
    > horLineLength, horLineStartPoi nt.Y));
    > g.DrawLine(pen, vertLineStartPo int,new Point(vertLineS tartPoint.X,
    > vertLineStartPo int.Y + vertLineLength) );
    >
    > // Skriv ut texterna...
    > StringFormat stringformat = new StringFormat();
    > stringformat.Li neAlignment = StringAlignment .Center;
    > stringformat.Al ignment = StringAlignment .Center;
    > Brush stringBrush = Brushes.Black;
    > int LineSpacing = 0;
    >
    > Font captionFont = new Font("Arial",14 .0F,FontStyle.B old);
    > RectangleF captionRect = new RectangleF(new
    > PointF(horLineS tartPoint.X,0.0 F),new[/color]
    SizeF(horLineLe ngth,captionFon t.Height[color=blue]
    > + LineSpacing));
    > g.FillRectangle (Brushes.LightC yan,captionRect );
    >[/color]
    g.DrawString(st rCaption,captio nFont,stringBru sh,captionRect, stringformat);[color=blue]
    >
    > Font periodFont = new Font("Arial",10 .0F);
    > RectangleF periodRect = new RectangleF(new
    > PointF(horLineS tartPoint.X,cap tionFont.Height ),new
    > SizeF(horLineLe ngth,periodFont .Height + LineSpacing));
    > g.FillRectangle (Brushes.LightG reen,periodRect );
    > g.DrawString(st rPeriod,periodF ont,stringBrush ,periodRect,str ingformat);
    >
    > Font leftrightFont = new Font("Arial",9. 0F);
    >
    > RectangleF leftRect = new RectangleF(new
    > PointF(horLineS tartPoint.X,cap tionFont.Height +periodFont.Hei ght),new
    > SizeF(horLineLe ngth/2,leftrightFont .Height + LineSpacing));
    > stringformat.Al ignment = StringAlignment .Near;
    > g.FillRectangle (Brushes.LightP ink,leftRect);
    >[/color]
    g.DrawString(st rLeftText,leftr ightFont,string Brush,leftRect, stringformat);[color=blue]
    >
    > RectangleF rightRect = new RectangleF(new
    >[/color]
    PointF(horLineS tartPoint.X+hor LineLength/2,captionFont.H eight+periodFon t.Hei[color=blue]
    > ght),new SizeF(horLineLe ngth/2,leftrightFont .Height + LineSpacing));
    > stringformat.Al ignment = StringAlignment .Far;
    > g.FillRectangle (Brushes.LightS almon,rightRect );
    >
    >[/color]
    g.DrawString(st rRightText,left rightFont,strin gBrush,rightRec t,stringformat) ;[color=blue]
    > }
    >
    >
    > Thanks for your help!
    >
    > /David.
    >
    >[/color]


    Comment

    Working...