Groups | Blog | Home
all groups > dotnet drawing api > june 2005 >

dotnet drawing api : Two newbie questions



zhaounknown
6/24/2005 12:28:04 AM
1. PaintEventArgs.Graphics.Drawstring() need to specify the upper-left
coordinate for the text to be drawn. What measure unit should be used for the
coordinate?

2. Why the following piece of code can't print the Texts (on letter paper)?
However, if remove the "e.Graphics.PageUnit=GraphicsUnit.Inch;" and set
0.5F/0.6F to be 0.5*100 and 0.6*100 can work.

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
Font newFont= new System.Drawing.Font("Arial", 10F,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,
((System.Byte)(134)));

e.Graphics.PageUnit=GraphicsUnit.Inch;//
float
leftInch=(float)(this.textBox1.Left*1.0/this.pictureBox1.Image.Width*8.5);
float
topInch=(float)(this.textBox1.Top*1.0/this.pictureBox1.Image.Height*11.0);

e.Graphics.DrawString(this.textBox1.Text, this.textBox1.Font, Brushes.Black,
leftInch, topInch);

e.Graphics.DrawString("X", newFont, Brushes.Black, 0.5F, 0.6F);
zhaounknown
6/24/2005 8:10:03 AM
When I comment out to be the following, it still doesnot work. The
leftInch=3.5 inch, the topInch=3.8inch. When I further comment out the
leftInch and rightInch statements, it prints. What's the reason?

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
Font newFont= new System.Drawing.Font("Arial", 10F,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,
((System.Byte)(134)));

e.Graphics.PageUnit=GraphicsUnit.Inch;//
float
leftInch=(float)(this.textBox1.Left*1.0/this.pictureBox1.Image.Width*8.5);
float
topInch=(float)(this.textBox1.Top*1.0/this.pictureBox1.Image.Height*11.0);

e.Graphics.DrawString("X", newFont, Brushes.Black, 0.5F, 0.6F);
}

Another question: the destination and source rectangles in
PaintEventrgs.DrawImage() should be in measure unit defined by the
GraphicsUnit parameter or by the external e.Graphics.PageUnit?

Does DrawString takes the rectangle or pointF parameters in measure unit
defined by Font's emsize generaly in Point or by the external
e.Graphics.PageUnit?

[quoted text, click to view]
Bob Powell [MVP]
6/24/2005 10:50:06 AM
If you're printing then a real-world measurement system such as points,
millimeters or inches is best.

Remember that the printer bounds are declared in 100ths of an inch. NOT
pixels. You need to multiply the printer bounds by the DPI to get the real
pixel count if you want to use pixels at-all.

I haven't the faintest idea what sort of values you're producing with your
calculations on how the reciprocal width of a picturebox has a relation to
the left edge of a textbox. This is more esoteric than quantum physics. The
text doesn't show up because the result of that calculation puts it outside
the page margins obviously.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





[quoted text, click to view]

Bob Powell [MVP]
6/24/2005 7:23:02 PM
Hmm, I suspect that you're actually throwing an exception because of a
divide by zero or some such thing and catching it someplace else. Have you
put an application exception handler in somewhere?

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





[quoted text, click to view]

AddThis Social Bookmark Button