473,395 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Possible alignment bug in DrawString() ??

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,ca ptionRect,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,peri odRect,stringformat);

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

RectangleF leftRect = new RectangleF(new
PointF(horLineStartPoint.X,captionFont.Height+peri odFont.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,stringBrus h,rightRect,stringformat);
}
Thanks for your help!

/David.
Nov 15 '05 #1
1 4342
It seems www.lowrad.net is down for some time (I don't know how long).
Therefore the screenshot is also available here:
http://elite.csbnet.se/~skibum/alignment_screenshot.jpg

/David.

"David Lindgren" <da************@hogia.se.REMOVE!> skrev i meddelandet
news:uC**************@TK2MSFTNGP12.phx.gbl...
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,ca ptionRect,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,peri odRect,stringformat);

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

RectangleF leftRect = new RectangleF(new
PointF(horLineStartPoint.X,captionFont.Height+peri odFont.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,stringBrus h,rightRect,stringformat); }
Thanks for your help!

/David.

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Harpreet Singh via .NET 247 | last post by:
(Type your message here) Hi I want to align the text to be printed to be right aligned. I used stringalignment.far property but it instead started printing vertically from up to down.. While near...
2
by: John Smith | last post by:
Hi all; Putting "Due" into the column header of a datagrid. Font is a proportional fort. When the alignment is left, there is some space between the column separator bar and the D in Due....
7
by: Earl | last post by:
Any known fixes for the wacky right-alignment bug in the WinForms datagrid (VS2003)? I've tried Ken's workaround...
7
by: Doug Bell | last post by:
Hi Does anyone know (or point me where I can find) how to set the alignment of a DataGrid Column Header different to the alignment of the column. I am trying to show some Right aligned columns...
1
by: Joao | last post by:
Hi, I'm trully excited to use VB.NET's PrintDocument to make my printouts. On them I draw strings, images and lines. All perfect, if I invoke the Print or the PrintPreview one time per run. At...
0
by: VorTechS | last post by:
I'm having a problem with an inherited label, applying text rotation to aligned text. If text rotation is applied to the aligned text, the alignment goes 'nuts'. I can find no logic to what is...
11
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I know I sound like a one-note Johnny on this but I'm still looking for a solution. I need to display characters coming in from a serial port or a socket. I also need to be able to type...
2
by: ChrisNightingale | last post by:
Hi everybody, I have an odd issue which I'm not sure how to resolve. I'm basically implementing a print mechanism which takes a series of controls and reproduces them on a print document. So...
2
by: Tony Johansson | last post by:
Hello! If I use the DrawString below with object of StringFormat as the last object it works good. If I instead remove object StringFormat below as the last object of DrawString I get some rows...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.