473,797 Members | 3,187 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:
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,"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.
Nov 15 '05 #1
1 4364
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.REMOV E!> skrev i meddelandet
news:uC******** ******@TK2MSFTN GP12.phx.gbl...
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 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.

Nov 15 '05 #2

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

Similar topics

0
1023
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 and center seems to work fine. BoxRectangle = New System.Drawing.RectangleF(Left, top, Width, Height) AlignmentLabel = New System.Drawing.StringFormat(System.Drawing.StringAlignment.Far) e.Graphics.DrawString(Value, FontLabel,...
2
8094
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. When the alignment is center, Due is centered in the header box. When the alignment is right, the e in Due is partially covered by the column separator bar. Adding space after Due (i.e.. "Due ") does not work. HeaderText seems to be trimmed before...
7
2040
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 (http://www.windowsformsdatagridhelp.com/default.aspx?ID=4bfab32d-9cff-4f5c-ba95-49bb9074a8bc), but I get no alignment at all when calling the class. George Shephard's site, while imminently useful, does not have an anwer for this issue.
7
1976
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 and the header looks wrong squashed to the right. If I could even add a trailing space but it trims any trailing spaces off. Thanks
1
1990
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 second time I onvoke print or preview, the DrawLine or DrawImage go well, but the first DrawString the program tries to execute pops an exception: ************** Exception Text ************** System.ArgumentException: Invalid parameter used.
0
1806
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 happening. I've built the following code from several examples on the web, if you remove the rotation then alignment works fine: Imports System.ComponentModel
11
7655
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 characters into the display myself - but that's not the main issue at this time. I've tried a scrolling multiline text box but once the original viewable area fills up and it starts scrolling the flashing of the entire area drives me nuts. The...
2
2543
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 what I'm trying to do is print out a label so what I basically did is use Graphics.DrawString passing in the text of the label, the font of the label and the rectangle which represents the label (because it's set to autosize this should be the right...
2
12664
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 that are not printed correctly. It's look like when toner is too low for the printer.This is only for some rows. Can somebody explain why this happens? What kind of StringFormat is used when this parameter is missing?
0
9537
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9066
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7560
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3750
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2934
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.