MeasureString results for printer GDI+

MeasureString results for printer GDI+

Post by Bill » Wed, 28 May 2003 23:31:16



The documentation for MeasureString states it returns the
number of pixels the string will require.  How do I
relate that to a printer?  If I consider it as DPI the
measurement is not correct.  Do I get the DPI of the
screen and use that as the pixels per inch and then
compute the printed string size?
 
 
 

MeasureString results for printer GDI+

Post by Greg Binkerd [M » Thu, 29 May 2003 02:14:58


Hi,

The units returned are probably in 1/100 inch units (or whatever the
Graphics object is set to in your PrintPage event handler).

You may want to check to see what your pea.Graphics.VisibleClipBounds.Width
is and determine the units.

The following code will call MeasureString to try and center the text on
the paper:

                void OnPrintPage( object obj, PrintPageEventArgs pea )
                {
                        Font     font  = new Font("Times New Roman", 10);
                        string   str   = "Test from Greg";
                        SizeF    sizef = pea.Graphics.MeasureString(str, font);

                        pea.Graphics.DrawString(str, font, Brushes.Black,
                                (pea.Graphics.VisibleClipBounds.Width - sizef.Width) / 2,
                                (pea.Graphics.VisibleClipBounds.Height - sizef.Height) / 2);
                        pea.HasMorePages = false;
                        nPageNumber++;
                }

Thanks,
-Greg
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Visit http://www.microsoft.com/security for current information on security.

--------------------
| Content-Class: urn:content-classes:message


| Subject: MeasureString results for printer GDI+
| Date: Tue, 27 May 2003 07:31:16 -0700
| Lines: 6

| MIME-Version: 1.0
| Content-Type: text/plain;
|       charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Thread-Index: AcMkXKH3jKdI1v5oRaqnvPfGsgv9KQ==
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Newsgroups: microsoft.public.dotnet.framework.drawing
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.drawing:6778
| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
| X-Tomcat-NG: microsoft.public.dotnet.framework.drawing
|
| The documentation for MeasureString states it returns the
| number of pixels the string will require.  How do I
| relate that to a printer?  If I consider it as DPI the
| measurement is not correct.  Do I get the DPI of the
| screen and use that as the pixels per inch and then
| compute the printed string size?
|

 
 
 

MeasureString results for printer GDI+

Post by Bill B » Thu, 29 May 2003 04:58:47


It's easy when you measure the string and then print with
the font.  I am trying to find a way to calculate the
font to print.  There are no examples doing this.  The
PageUnit in my Graphic object is Display units which are
defined as 1/75 inch.  With this calculation I cannot
determine a font width that works correctly.
Quote:>-----Original Message-----
>Hi,

>The units returned are probably in 1/100 inch units (or
whatever the
>Graphics object is set to in your PrintPage event
handler).

>You may want to check to see what your

pea.Graphics.VisibleClipBounds.Width
Quote:>is and determine the units.

>The following code will call MeasureString to try and
center the text on
>the paper:

>            void OnPrintPage( object obj,

PrintPageEventArgs pea )
Quote:>            {
>                    Font     font  = new Font("Times
New Roman", 10);
>                    string   str   = "Test from Greg";
>                    SizeF    sizef =

pea.Graphics.MeasureString(str, font);
Quote:

>                    pea.Graphics.DrawString(str,

font, Brushes.Black,
        (pea.Graphics.VisibleClipBounds.Width -
sizef.Width) / 2,
        (pea.Graphics.VisibleClipBounds.Height -
sizef.Height) / 2);
Quote:>                    pea.HasMorePages = false;
>                    nPageNumber++;
>            }

>Thanks,
>-Greg
>Microsoft Developer Support

>This posting is provided "AS IS" with no warranties, and
confers no rights.
>Visit http://www.microsoft.com/security for current

information on security.

>--------------------
>| Content-Class: urn:content-classes:message


>| Subject: MeasureString results for printer GDI+
>| Date: Tue, 27 May 2003 07:31:16 -0700
>| Lines: 6

>| MIME-Version: 1.0
>| Content-Type: text/plain;
>|   charset="iso-8859-1"
>| Content-Transfer-Encoding: 7bit
>| X-Newsreader: Microsoft CDO for Windows 2000
>| Thread-Index: AcMkXKH3jKdI1v5oRaqnvPfGsgv9KQ==
>| X-MimeOLE: Produced By Microsoft MimeOLE
V5.50.4910.0300
>| Newsgroups: microsoft.public.dotnet.framework.drawing
>| Path: cpmsftngxa06.phx.gbl
>| Xref: cpmsftngxa06.phx.gbl

microsoft.public.dotnet.framework.drawing:6778

- Show quoted text -

Quote:>| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
>| X-Tomcat-NG: microsoft.public.dotnet.framework.drawing
>|
>| The documentation for MeasureString states it returns
the
>| number of pixels the string will require.  How do I
>| relate that to a printer?  If I consider it as DPI the
>| measurement is not correct.  Do I get the DPI of the
>| screen and use that as the pixels per inch and then
>| compute the printed string size?
>|

>.

 
 
 

MeasureString results for printer GDI+

Post by Ron Alle » Thu, 29 May 2003 05:14:01


Bill,
    Every time I've measured strings the results are returned in default
units which are 1/100ths of an inch which is approximately 1 pixel for most
(96ppi) displays.  Unless you have changed your measurement units on the
graphics I would suspect that you are getting back these units.

Ron Allen

> It's easy when you measure the string and then print with
> the font.  I am trying to find a way to calculate the
> font to print.  There are no examples doing this.  The
> PageUnit in my Graphic object is Display units which are
> defined as 1/75 inch.  With this calculation I cannot
> determine a font width that works correctly.
> >-----Original Message-----
> >Hi,

> >The units returned are probably in 1/100 inch units (or
> whatever the
> >Graphics object is set to in your PrintPage event
> handler).

> >You may want to check to see what your
> pea.Graphics.VisibleClipBounds.Width
> >is and determine the units.

> >The following code will call MeasureString to try and
> center the text on
> >the paper:

> > void OnPrintPage( object obj,
> PrintPageEventArgs pea )
> > {
> > Font     font  = new Font("Times
> New Roman", 10);
> > string   str   = "Test from Greg";
> > SizeF    sizef =
> pea.Graphics.MeasureString(str, font);

> > pea.Graphics.DrawString(str,
> font, Brushes.Black,

> (pea.Graphics.VisibleClipBounds.Width -
> sizef.Width) / 2,

> (pea.Graphics.VisibleClipBounds.Height -
> sizef.Height) / 2);
> > pea.HasMorePages = false;
> > nPageNumber++;
> > }

> >Thanks,
> >-Greg
> >Microsoft Developer Support

> >This posting is provided "AS IS" with no warranties, and
> confers no rights.
> >Visit http://www.microsoft.com/security for current
> information on security.

> >--------------------
> >| Content-Class: urn:content-classes:message


> >| Subject: MeasureString results for printer GDI+
> >| Date: Tue, 27 May 2003 07:31:16 -0700
> >| Lines: 6

> >| MIME-Version: 1.0
> >| Content-Type: text/plain;
> >| charset="iso-8859-1"
> >| Content-Transfer-Encoding: 7bit
> >| X-Newsreader: Microsoft CDO for Windows 2000
> >| Thread-Index: AcMkXKH3jKdI1v5oRaqnvPfGsgv9KQ==
> >| X-MimeOLE: Produced By Microsoft MimeOLE
> V5.50.4910.0300
> >| Newsgroups: microsoft.public.dotnet.framework.drawing
> >| Path: cpmsftngxa06.phx.gbl
> >| Xref: cpmsftngxa06.phx.gbl
> microsoft.public.dotnet.framework.drawing:6778
> >| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
> >| X-Tomcat-NG: microsoft.public.dotnet.framework.drawing
> >|
> >| The documentation for MeasureString states it returns
> the
> >| number of pixels the string will require.  How do I
> >| relate that to a printer?  If I consider it as DPI the
> >| measurement is not correct.  Do I get the DPI of the
> >| screen and use that as the pixels per inch and then
> >| compute the printed string size?
> >|

> >.

 
 
 

MeasureString results for printer GDI+

Post by Bill » Thu, 29 May 2003 05:45:46


Ron,

Thanks for the advice.  Why does the .NET help on
GraphicsUnit define the Display enum as 1/75 inch?  
Anyway, the advice didn't help.  The problem is I can't
define a calculation to use to determine a font to use to
print with.  I have a printer with 300DPI and 132
characters to print.  The calcuation is simple(or should
be):

NumberOfPointsPerCharacter = (NumberOfInches *
72ppi)/NumberofCharactersToPrint

font = new Font("Courier
New",NumberOfPointsPerCharacter,GraphicsUnit.Point);

This does not work.  The font size is about 60% of the
size I need.  Creating an 8 point font gives the
approximate size needed but I can't find any way to
arrive at 8ppi by any calculation.  This should be easy.  
What am I missing???

Quote:>-----Original Message-----
>Bill,
>    Every time I've measured strings the results are
returned in default
>units which are 1/100ths of an inch which is

approximately 1 pixel for most
Quote:>(96ppi) displays.  Unless you have changed your

measurement units on the
>graphics I would suspect that you are getting back these
units.

>Ron Allen


>> It's easy when you measure the string and then print
with
>> the font.  I am trying to find a way to calculate the
>> font to print.  There are no examples doing this.  The
>> PageUnit in my Graphic object is Display units which
are
>> defined as 1/75 inch.  With this calculation I cannot
>> determine a font width that works correctly.
>> >-----Original Message-----
>> >Hi,

>> >The units returned are probably in 1/100 inch units
(or
>> whatever the
>> >Graphics object is set to in your PrintPage event
>> handler).

>> >You may want to check to see what your
>> pea.Graphics.VisibleClipBounds.Width
>> >is and determine the units.

>> >The following code will call MeasureString to try and
>> center the text on
>> >the paper:

>> > void OnPrintPage( object obj,
>> PrintPageEventArgs pea )
>> > {
>> > Font     font  = new Font("Times
>> New Roman", 10);
>> > string   str   = "Test from Greg";
>> > SizeF    sizef =
>> pea.Graphics.MeasureString(str, font);

>> > pea.Graphics.DrawString(str,
>> font, Brushes.Black,

>> (pea.Graphics.VisibleClipBounds.Width -
>> sizef.Width) / 2,

>> (pea.Graphics.VisibleClipBounds.Height -
>> sizef.Height) / 2);
>> > pea.HasMorePages = false;
>> > nPageNumber++;
>> > }

>> >Thanks,
>> >-Greg
>> >Microsoft Developer Support

>> >This posting is provided "AS IS" with no warranties,
and
>> confers no rights.
>> >Visit http://www.microsoft.com/security for current
>> information on security.

>> >--------------------
>> >| Content-Class: urn:content-classes:message


>> >| Subject: MeasureString results for printer GDI+
>> >| Date: Tue, 27 May 2003 07:31:16 -0700
>> >| Lines: 6

>> >| MIME-Version: 1.0
>> >| Content-Type: text/plain;
>> >| charset="iso-8859-1"
>> >| Content-Transfer-Encoding: 7bit
>> >| X-Newsreader: Microsoft CDO for Windows 2000
>> >| Thread-Index: AcMkXKH3jKdI1v5oRaqnvPfGsgv9KQ==
>> >| X-MimeOLE: Produced By Microsoft MimeOLE
>> V5.50.4910.0300
>> >| Newsgroups:

microsoft.public.dotnet.framework.drawing
Quote:>> >| Path: cpmsftngxa06.phx.gbl
>> >| Xref: cpmsftngxa06.phx.gbl
>> microsoft.public.dotnet.framework.drawing:6778
>> >| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
>> >| X-Tomcat-NG:

microsoft.public.dotnet.framework.drawing

- Show quoted text -

Quote:>> >|
>> >| The documentation for MeasureString states it
returns
>> the
>> >| number of pixels the string will require.  How do I
>> >| relate that to a printer?  If I consider it as DPI
the
>> >| measurement is not correct.  Do I get the DPI of the
>> >| screen and use that as the pixels per inch and then
>> >| compute the printed string size?
>> >|

>> >.

>.

 
 
 

MeasureString results for printer GDI+

Post by Bob Powell [MVP » Thu, 29 May 2003 20:05:40


Having done this on several occasions before, I find that the best way is to
switch the Graphics page units over to points or millimeters and do all
measurement in an absolute linear scale rather than try to equate printer,
screen, declared DPI and actual DPI. The resulting dimensions will work
equally on screen or printer.

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

Check out the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Buy quality Windows Forms tools
http://www.bobpowell.net/xray_tools.htm


Quote:> The documentation for MeasureString states it returns the
> number of pixels the string will require.  How do I
> relate that to a printer?  If I consider it as DPI the
> measurement is not correct.  Do I get the DPI of the
> screen and use that as the pixels per inch and then
> compute the printed string size?

 
 
 

MeasureString results for printer GDI+

Post by Bil B » Thu, 29 May 2003 22:05:44


Bob,

Thanks for the info.  I have tried this technique based
on your advice but I don't see any change in the output.  
During the Paint message procession, I change the
PageUnit to GraphicsUnit.Point and then use DrawString to
paint the page.  The results are the same as when I don't
change the PageUnit.  Is there something else I should be
doing?

Thanks,
Bill B

Quote:>-----Original Message-----
>Having done this on several occasions before, I find

that the best way is to
Quote:>switch the Graphics page units over to points or

millimeters and do all
Quote:>measurement in an absolute linear scale rather than try
to equate printer,
>screen, declared DPI and actual DPI. The resulting

dimensions will work
>equally on screen or printer.

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

>Check out the GDI+ FAQ
>http://www.bobpowell.net/gdiplus_faq.htm

>Buy quality Windows Forms tools
>http://www.bobpowell.net/xray_tools.htm



>> The documentation for MeasureString states it returns
the
>> number of pixels the string will require.  How do I
>> relate that to a printer?  If I consider it as DPI the
>> measurement is not correct.  Do I get the DPI of the
>> screen and use that as the pixels per inch and then
>> compute the printed string size?

>.

 
 
 

MeasureString results for printer GDI+

Post by Bob Powell [MVP » Fri, 30 May 2003 00:12:06


1..Do you switch the graphics units to points when you measure the strings?

2..Can you post a bit of code so we can peruse it?

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

Check out the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Buy quality Windows Forms tools
http://www.bobpowell.net/xray_tools.htm


> Bob,

> Thanks for the info.  I have tried this technique based
> on your advice but I don't see any change in the output.
> During the Paint message procession, I change the
> PageUnit to GraphicsUnit.Point and then use DrawString to
> paint the page.  The results are the same as when I don't
> change the PageUnit.  Is there something else I should be
> doing?

> Thanks,
> Bill B
> >-----Original Message-----
> >Having done this on several occasions before, I find
> that the best way is to
> >switch the Graphics page units over to points or
> millimeters and do all
> >measurement in an absolute linear scale rather than try
> to equate printer,
> >screen, declared DPI and actual DPI. The resulting
> dimensions will work
> >equally on screen or printer.

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

> >Check out the GDI+ FAQ
> >http://www.bobpowell.net/gdiplus_faq.htm

> >Buy quality Windows Forms tools
> >http://www.bobpowell.net/xray_tools.htm



> >> The documentation for MeasureString states it returns
> the
> >> number of pixels the string will require.  How do I
> >> relate that to a printer?  If I consider it as DPI the
> >> measurement is not correct.  Do I get the DPI of the
> >> screen and use that as the pixels per inch and then
> >> compute the printed string size?

> >.

 
 
 

MeasureString results for printer GDI+

Post by Bill B » Fri, 30 May 2003 00:30:03


Bob,

The code below is what I use to test the character size.  
The strings I am printing are 132 characters in length.  
What I am trying to accomplish is to have the 132
characters fill the printable area.  Regardless of the
PageUnit, the characters only fill about 2/3 of the print
line.  I am using a Print Preview Dialog with landscape
orientation. Thanks again for any help you can offer.

                protected override void OnBeginPrint
(PrintEventArgs ev)
                {
                        base.OnBeginPrint(ev);
                        this.m_printFont =
this.m_currentFont;
                        Rectangle printRect =
this.DefaultPageSettings.Bounds;
                        printRect.Width -=
(this.DefaultPageSettings.Margins.Left +
this.DefaultPageSettings.Margins.Right);
                        float printWidthInInches = (float)
(printRect.Width / 100);
                        float printWidthInPoints =
printWidthInInches * 72f;
                        float charWidthInPoints =
printWidthInPoints / 132f;
                        this.m_printFont = new Font
("Courier New",charWidthInPoints,GraphicsUnit.Point);

                }
                protected override void OnPrintPage
(PrintPageEventArgs e)
                {
                        float LeftMargin =
e.MarginBounds.Left;
                        float TopMargin =
e.MarginBounds.Top;

                        e.Graphics.PageUnit =
GraphicsUnit.Point;
                        base.OnPrintPage(e);
                        string text =
this.m_contentPage.Substring(0,132);
                        e.Graphics.DrawString
(text,this.m_printFont,Brushes.Black,LeftMargin,TopMargin)
;
                        text =
this.m_contentPage.Substring(132,132);
                        e.Graphics.DrawString
(text,this.m_printFont,Brushes.Black,LeftMargin,TopMargin
+ this.m_currentFont.GetHeight());
                }

Quote:>-----Original Message-----
>1..Do you switch the graphics units to points when you

measure the strings?

>2..Can you post a bit of code so we can peruse it?

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

>Check out the GDI+ FAQ
>http://www.bobpowell.net/gdiplus_faq.htm

>Buy quality Windows Forms tools
>http://www.bobpowell.net/xray_tools.htm



>> Bob,

>> Thanks for the info.  I have tried this technique based
>> on your advice but I don't see any change in the
output.
>> During the Paint message procession, I change the
>> PageUnit to GraphicsUnit.Point and then use DrawString
to
>> paint the page.  The results are the same as when I
don't
>> change the PageUnit.  Is there something else I should
be
>> doing?

>> Thanks,
>> Bill B
>> >-----Original Message-----
>> >Having done this on several occasions before, I find
>> that the best way is to
>> >switch the Graphics page units over to points or
>> millimeters and do all
>> >measurement in an absolute linear scale rather than
try
>> to equate printer,
>> >screen, declared DPI and actual DPI. The resulting
>> dimensions will work
>> >equally on screen or printer.

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

>> >Check out the GDI+ FAQ
>> >http://www.bobpowell.net/gdiplus_faq.htm

>> >Buy quality Windows Forms tools
>> >http://www.bobpowell.net/xray_tools.htm



>> >> The documentation for MeasureString states it
returns
>> the
>> >> number of pixels the string will require.  How do I
>> >> relate that to a printer?  If I consider it as DPI
the
>> >> measurement is not correct.  Do I get the DPI of the
>> >> screen and use that as the pixels per inch and then
>> >> compute the printed string size?

>> >.

>.

 
 
 

MeasureString results for printer GDI+

Post by Rick » Fri, 30 May 2003 02:07:21


one thing ive noticed about the page units is that it seems to ignore it if
you use the font object of a textbox or other control, but if you create a
new font object with the same properties as the one from the control, it
will work just fine, and use the page units you specify.



> 1..Do you switch the graphics units to points when you measure the
strings?

> 2..Can you post a bit of code so we can peruse it?

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

> Check out the GDI+ FAQ
> http://www.bobpowell.net/gdiplus_faq.htm

> Buy quality Windows Forms tools
> http://www.bobpowell.net/xray_tools.htm



> > Bob,

> > Thanks for the info.  I have tried this technique based
> > on your advice but I don't see any change in the output.
> > During the Paint message procession, I change the
> > PageUnit to GraphicsUnit.Point and then use DrawString to
> > paint the page.  The results are the same as when I don't
> > change the PageUnit.  Is there something else I should be
> > doing?

> > Thanks,
> > Bill B
> > >-----Original Message-----
> > >Having done this on several occasions before, I find
> > that the best way is to
> > >switch the Graphics page units over to points or
> > millimeters and do all
> > >measurement in an absolute linear scale rather than try
> > to equate printer,
> > >screen, declared DPI and actual DPI. The resulting
> > dimensions will work
> > >equally on screen or printer.

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

> > >Check out the GDI+ FAQ
> > >http://www.bobpowell.net/gdiplus_faq.htm

> > >Buy quality Windows Forms tools
> > >http://www.bobpowell.net/xray_tools.htm



> > >> The documentation for MeasureString states it returns
> > the
> > >> number of pixels the string will require.  How do I
> > >> relate that to a printer?  If I consider it as DPI the
> > >> measurement is not correct.  Do I get the DPI of the
> > >> screen and use that as the pixels per inch and then
> > >> compute the printed string size?

> > >.

 
 
 

MeasureString results for printer GDI+

Post by Ron Alle » Fri, 30 May 2003 03:21:12


Bill,
    When you divide the rectangle width by 100 you will get integer
division.  I think you need
printWidthInInches = ((float)printRect.Width)/100.0f;
to avoid truncating the width with the division.

Ron Allen

> Bob,

> The code below is what I use to test the character size.
> The strings I am printing are 132 characters in length.
> What I am trying to accomplish is to have the 132
> characters fill the printable area.  Regardless of the
> PageUnit, the characters only fill about 2/3 of the print
> line.  I am using a Print Preview Dialog with landscape
> orientation. Thanks again for any help you can offer.

> protected override void OnBeginPrint
> (PrintEventArgs ev)
> {
> base.OnBeginPrint(ev);
> this.m_printFont =
> this.m_currentFont;
> Rectangle printRect =
> this.DefaultPageSettings.Bounds;
> printRect.Width -=
> (this.DefaultPageSettings.Margins.Left +
> this.DefaultPageSettings.Margins.Right);
> float printWidthInInches = (float)
> (printRect.Width / 100);
> float printWidthInPoints =
> printWidthInInches * 72f;
> float charWidthInPoints =
> printWidthInPoints / 132f;
> this.m_printFont = new Font
> ("Courier New",charWidthInPoints,GraphicsUnit.Point);

> }
> protected override void OnPrintPage
> (PrintPageEventArgs e)
> {
> float LeftMargin =
> e.MarginBounds.Left;
> float TopMargin =
> e.MarginBounds.Top;

> e.Graphics.PageUnit =
> GraphicsUnit.Point;
> base.OnPrintPage(e);
> string text =
> this.m_contentPage.Substring(0,132);
> e.Graphics.DrawString
> (text,this.m_printFont,Brushes.Black,LeftMargin,TopMargin)
> ;
> text =
> this.m_contentPage.Substring(132,132);
> e.Graphics.DrawString
> (text,this.m_printFont,Brushes.Black,LeftMargin,TopMargin
> + this.m_currentFont.GetHeight());
> }

> >-----Original Message-----
> >1..Do you switch the graphics units to points when you
> measure the strings?

> >2..Can you post a bit of code so we can peruse it?

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

> >Check out the GDI+ FAQ
> >http://www.bobpowell.net/gdiplus_faq.htm

> >Buy quality Windows Forms tools
> >http://www.bobpowell.net/xray_tools.htm



> >> Bob,

> >> Thanks for the info.  I have tried this technique based
> >> on your advice but I don't see any change in the
> output.
> >> During the Paint message procession, I change the
> >> PageUnit to GraphicsUnit.Point and then use DrawString
> to
> >> paint the page.  The results are the same as when I
> don't
> >> change the PageUnit.  Is there something else I should
> be
> >> doing?

> >> Thanks,
> >> Bill B
> >> >-----Original Message-----
> >> >Having done this on several occasions before, I find
> >> that the best way is to
> >> >switch the Graphics page units over to points or
> >> millimeters and do all
> >> >measurement in an absolute linear scale rather than
> try
> >> to equate printer,
> >> >screen, declared DPI and actual DPI. The resulting
> >> dimensions will work
> >> >equally on screen or printer.

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

> >> >Check out the GDI+ FAQ
> >> >http://www.bobpowell.net/gdiplus_faq.htm

> >> >Buy quality Windows Forms tools
> >> >http://www.bobpowell.net/xray_tools.htm



> >> >> The documentation for MeasureString states it
> returns
> >> the
> >> >> number of pixels the string will require.  How do I
> >> >> relate that to a printer?  If I consider it as DPI
> the
> >> >> measurement is not correct.  Do I get the DPI of the
> >> >> screen and use that as the pixels per inch and then
> >> >> compute the printed string size?

> >> >.

> >.

 
 
 

MeasureString results for printer GDI+

Post by Bill » Fri, 30 May 2003 03:58:54


Ron,

You are correct but in my test program it divides evenly
(900 / 100) so the error does not affect the problem I
have.  Thanks for your help.
Bill

>-----Original Message-----
>Bill,
>    When you divide the rectangle width by 100 you will
get integer
>division.  I think you need
>printWidthInInches = ((float)printRect.Width)/100.0f;
>to avoid truncating the width with the division.

>Ron Allen


>> Bob,

>> The code below is what I use to test the character
size.
>> The strings I am printing are 132 characters in length.
>> What I am trying to accomplish is to have the 132
>> characters fill the printable area.  Regardless of the
>> PageUnit, the characters only fill about 2/3 of the
print
>> line.  I am using a Print Preview Dialog with landscape
>> orientation. Thanks again for any help you can offer.

>> protected override void OnBeginPrint
>> (PrintEventArgs ev)
>> {
>> base.OnBeginPrint(ev);
>> this.m_printFont =
>> this.m_currentFont;
>> Rectangle printRect =
>> this.DefaultPageSettings.Bounds;
>> printRect.Width -=
>> (this.DefaultPageSettings.Margins.Left +
>> this.DefaultPageSettings.Margins.Right);
>> float printWidthInInches = (float)
>> (printRect.Width / 100);
>> float printWidthInPoints =
>> printWidthInInches * 72f;
>> float charWidthInPoints =
>> printWidthInPoints / 132f;
>> this.m_printFont = new Font
>> ("Courier New",charWidthInPoints,GraphicsUnit.Point);

>> }
>> protected override void OnPrintPage
>> (PrintPageEventArgs e)
>> {
>> float LeftMargin =
>> e.MarginBounds.Left;
>> float TopMargin =
>> e.MarginBounds.Top;

>> e.Graphics.PageUnit =
>> GraphicsUnit.Point;
>> base.OnPrintPage(e);
>> string text =
>> this.m_contentPage.Substring(0,132);
>> e.Graphics.DrawString

(text,this.m_printFont,Brushes.Black,LeftMargin,TopMargin)
Quote:>> ;
>> text =
>> this.m_contentPage.Substring(132,132);
>> e.Graphics.DrawString

(text,this.m_printFont,Brushes.Black,LeftMargin,TopMargin

- Show quoted text -

>> + this.m_currentFont.GetHeight());
>> }

>> >-----Original Message-----
>> >1..Do you switch the graphics units to points when you
>> measure the strings?

>> >2..Can you post a bit of code so we can peruse it?

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

>> >Check out the GDI+ FAQ
>> >http://www.bobpowell.net/gdiplus_faq.htm

>> >Buy quality Windows Forms tools
>> >http://www.bobpowell.net/xray_tools.htm



>> >> Bob,

>> >> Thanks for the info.  I have tried this technique
based
>> >> on your advice but I don't see any change in the
>> output.
>> >> During the Paint message procession, I change the
>> >> PageUnit to GraphicsUnit.Point and then use
DrawString
>> to
>> >> paint the page.  The results are the same as when I
>> don't
>> >> change the PageUnit.  Is there something else I
should
>> be
>> >> doing?

>> >> Thanks,
>> >> Bill B
>> >> >-----Original Message-----
>> >> >Having done this on several occasions before, I
find
>> >> that the best way is to
>> >> >switch the Graphics page units over to points or
>> >> millimeters and do all
>> >> >measurement in an absolute linear scale rather than
>> try
>> >> to equate printer,
>> >> >screen, declared DPI and actual DPI. The resulting
>> >> dimensions will work
>> >> >equally on screen or printer.

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

>> >> >Check out the GDI+ FAQ
>> >> >http://www.bobpowell.net/gdiplus_faq.htm

>> >> >Buy quality Windows Forms tools
>> >> >http://www.bobpowell.net/xray_tools.htm



>> >> >> The documentation for MeasureString states it
>> returns
>> >> the
>> >> >> number of pixels the string will require.  How
do I
>> >> >> relate that to a printer?  If I consider it as
DPI
>> the
>> >> >> measurement is not correct.  Do I get the DPI of
the
>> >> >> screen and use that as the pixels per inch and
then
>> >> >> compute the printed string size?

>> >> >.

>> >.

>.

 
 
 

MeasureString results for printer GDI+

Post by Ron Alle » Fri, 30 May 2003 05:28:57


Bill,
    Having looked at this more closely I see that you are setting the Font
height to the desired character width in points.  This won't result in a
Font with the desired width as Courier New isn't square aspect.  Since it is
a fixed point font you may be able to determine the ratio of baseline height
to desired width somehow (or look it up) but just setting the height to this
will result in a significantly smaller print width.  Doing a bit of
heuristic testing I get a ratio of 0.6000977f for the ratio of width to
height so dividing by this should be close.

Regards,
Ron Allen

> Ron,

> You are correct but in my test program it divides evenly
> (900 / 100) so the error does not affect the problem I
> have.  Thanks for your help.
> Bill
> >-----Original Message-----
> >Bill,
> >    When you divide the rectangle width by 100 you will
> get integer
> >division.  I think you need
> >printWidthInInches = ((float)printRect.Width)/100.0f;
> >to avoid truncating the width with the division.

> >Ron Allen


> >> Bob,

> >> The code below is what I use to test the character
> size.
> >> The strings I am printing are 132 characters in length.
> >> What I am trying to accomplish is to have the 132
> >> characters fill the printable area.  Regardless of the
> >> PageUnit, the characters only fill about 2/3 of the
> print
> >> line.  I am using a Print Preview Dialog with landscape
> >> orientation. Thanks again for any help you can offer.

> >> protected override void OnBeginPrint
> >> (PrintEventArgs ev)
> >> {
> >> base.OnBeginPrint(ev);
> >> this.m_printFont =
> >> this.m_currentFont;
> >> Rectangle printRect =
> >> this.DefaultPageSettings.Bounds;
> >> printRect.Width -=
> >> (this.DefaultPageSettings.Margins.Left +
> >> this.DefaultPageSettings.Margins.Right);
> >> float printWidthInInches = (float)
> >> (printRect.Width / 100);
> >> float printWidthInPoints =
> >> printWidthInInches * 72f;
> >> float charWidthInPoints =
> >> printWidthInPoints / 132f;
> >> this.m_printFont = new Font
> >> ("Courier New",charWidthInPoints,GraphicsUnit.Point);

> >> }
> >> protected override void OnPrintPage
> >> (PrintPageEventArgs e)
> >> {
> >> float LeftMargin =
> >> e.MarginBounds.Left;
> >> float TopMargin =
> >> e.MarginBounds.Top;

> >> e.Graphics.PageUnit =
> >> GraphicsUnit.Point;
> >> base.OnPrintPage(e);
> >> string text =
> >> this.m_contentPage.Substring(0,132);
> >> e.Graphics.DrawString

> (text,this.m_printFont,Brushes.Black,LeftMargin,TopMargin)
> >> ;
> >> text =
> >> this.m_contentPage.Substring(132,132);
> >> e.Graphics.DrawString

> (text,this.m_printFont,Brushes.Black,LeftMargin,TopMargin
> >> + this.m_currentFont.GetHeight());
> >> }

> >> >-----Original Message-----
> >> >1..Do you switch the graphics units to points when you
> >> measure the strings?

> >> >2..Can you post a bit of code so we can peruse it?

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

> >> >Check out the GDI+ FAQ
> >> >http://www.bobpowell.net/gdiplus_faq.htm

> >> >Buy quality Windows Forms tools
> >> >http://www.bobpowell.net/xray_tools.htm



> >> >> Bob,

> >> >> Thanks for the info.  I have tried this technique
> based
> >> >> on your advice but I don't see any change in the
> >> output.
> >> >> During the Paint message procession, I change the
> >> >> PageUnit to GraphicsUnit.Point and then use
> DrawString
> >> to
> >> >> paint the page.  The results are the same as when I
> >> don't
> >> >> change the PageUnit.  Is there something else I
> should
> >> be
> >> >> doing?

> >> >> Thanks,
> >> >> Bill B
> >> >> >-----Original Message-----
> >> >> >Having done this on several occasions before, I
> find
> >> >> that the best way is to
> >> >> >switch the Graphics page units over to points or
> >> >> millimeters and do all
> >> >> >measurement in an absolute linear scale rather than
> >> try
> >> >> to equate printer,
> >> >> >screen, declared DPI and actual DPI. The resulting
> >> >> dimensions will work
> >> >> >equally on screen or printer.

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

> >> >> >Check out the GDI+ FAQ
> >> >> >http://www.bobpowell.net/gdiplus_faq.htm

> >> >> >Buy quality Windows Forms tools
> >> >> >http://www.bobpowell.net/xray_tools.htm



> >> >> >> The documentation for MeasureString states it
> >> returns
> >> >> the
> >> >> >> number of pixels the string will require.  How
> do I
> >> >> >> relate that to a printer?  If I consider it as
> DPI
> >> the
> >> >> >> measurement is not correct.  Do I get the DPI of
> the
> >> >> >> screen and use that as the pixels per inch and
> then
> >> >> >> compute the printed string size?

> >> >> >.

> >> >.

> >.

 
 
 

MeasureString results for printer GDI+

Post by Bill » Fri, 30 May 2003 06:21:26


Ron,

If I understand you correctly, the size parameter on the
Font object constructor is specifiying the Height and not
the width for creation of the Font instance.  Therefore,
I should look at the Size and SizeInPoints to be Height
values.  Your .6000977f ratio is consistent with the
results I'm seeing.  Do you know of anyway to create a
font with a specific width like the LOGFONT structure in
the old days?

Thanks much for all your help, this will give me a path
to follow for now.
Bill B

Quote:>-----Original Message-----
>Bill,
>    Having looked at this more closely I see that you

are setting the Font
Quote:>height to the desired character width in points.  This
won't result in a
>Font with the desired width as Courier New isn't square

aspect.  Since it is
Quote:>a fixed point font you may be able to determine the

ratio of baseline height
Quote:>to desired width somehow (or look it up) but just

setting the height to this
>will result in a significantly smaller print width.  
Doing a bit of
>heuristic testing I get a ratio of 0.6000977f for the
ratio of width to
>height so dividing by this should be close.

>Regards,
>Ron Allen


>> Ron,

>> You are correct but in my test program it divides
evenly
>> (900 / 100) so the error does not affect the problem I
>> have.  Thanks for your help.
>> Bill
>> >-----Original Message-----
>> >Bill,
>> >    When you divide the rectangle width by 100 you
will
>> get integer
>> >division.  I think you need
>> >printWidthInInches = ((float)printRect.Width)/100.0f;
>> >to avoid truncating the width with the division.

>> >Ron Allen


>> >> Bob,

>> >> The code below is what I use to test the character
>> size.
>> >> The strings I am printing are 132 characters in
length.
>> >> What I am trying to accomplish is to have the 132
>> >> characters fill the printable area.  Regardless of
the
>> >> PageUnit, the characters only fill about 2/3 of the
>> print
>> >> line.  I am using a Print Preview Dialog with
landscape
>> >> orientation. Thanks again for any help you can
offer.

>> >> protected override void OnBeginPrint
>> >> (PrintEventArgs ev)
>> >> {
>> >> base.OnBeginPrint(ev);
>> >> this.m_printFont =
>> >> this.m_currentFont;
>> >> Rectangle printRect =
>> >> this.DefaultPageSettings.Bounds;
>> >> printRect.Width -=
>> >> (this.DefaultPageSettings.Margins.Left +
>> >> this.DefaultPageSettings.Margins.Right);
>> >> float printWidthInInches = (float)
>> >> (printRect.Width / 100);
>> >> float printWidthInPoints =
>> >> printWidthInInches * 72f;
>> >> float charWidthInPoints =
>> >> printWidthInPoints / 132f;
>> >> this.m_printFont = new Font
>> >> ("Courier

New",charWidthInPoints,GraphicsUnit.Point);

- Show quoted text -

Quote:

>> >> }
>> >> protected override void OnPrintPage
>> >> (PrintPageEventArgs e)
>> >> {
>> >> float LeftMargin =
>> >> e.MarginBounds.Left;
>> >> float TopMargin =
>> >> e.MarginBounds.Top;

>> >> e.Graphics.PageUnit =
>> >> GraphicsUnit.Point;
>> >> base.OnPrintPage(e);
>> >> string text =
>> >> this.m_contentPage.Substring(0,132);
>> >> e.Graphics.DrawString

(text,this.m_printFont,Brushes.Black,LeftMargin,TopMargin)
Quote:>> >> ;
>> >> text =
>> >> this.m_contentPage.Substring(132,132);
>> >> e.Graphics.DrawString

(text,this.m_printFont,Brushes.Black,LeftMargin,TopMargin

- Show quoted text -

>> >> + this.m_currentFont.GetHeight());
>> >> }

>> >> >-----Original Message-----
>> >> >1..Do you switch the graphics units to points when
you
>> >> measure the strings?

>> >> >2..Can you post a bit of code so we can peruse it?

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

>> >> >Check out the GDI+ FAQ
>> >> >http://www.bobpowell.net/gdiplus_faq.htm

>> >> >Buy quality Windows Forms tools
>> >> >http://www.bobpowell.net/xray_tools.htm



>> >> >> Bob,

>> >> >> Thanks for the info.  I have tried this technique
>> based
>> >> >> on your advice but I don't see any change in the
>> >> output.
>> >> >> During the Paint message procession, I change the
>> >> >> PageUnit to GraphicsUnit.Point and then use
>> DrawString
>> >> to
>> >> >> paint the page.  The results are the same as
when I
>> >> don't
>> >> >> change the PageUnit.  Is there something else I
>> should
>> >> be
>> >> >> doing?

>> >> >> Thanks,
>> >> >> Bill B
>> >> >> >-----Original Message-----
>> >> >> >Having done this on several occasions before, I
>> find
>> >> >> that the best way is to
>> >> >> >switch the Graphics page units over to points or
>> >> >> millimeters and do all
>> >> >> >measurement in an absolute linear scale rather
than
>> >> try
>> >> >> to equate printer,
>> >> >> >screen, declared DPI and actual DPI. The
resulting
>> >> >> dimensions will work
>> >> >> >equally on screen or printer.

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

>> >> >> >Check out the GDI+ FAQ
>> >> >> >http://www.bobpowell.net/gdiplus_faq.htm

>> >> >> >Buy quality Windows Forms tools
>> >> >> >http://www.bobpowell.net/xray_tools.htm



>> >> >> >> The documentation for MeasureString states it
>> >> returns
>> >> >> the
>> >> >> >> number of pixels the string will require.  How
>> do I
>> >> >> >> relate that to a printer?  If I consider it as
>> DPI
>> >> the
>> >> >> >> measurement is not correct.  Do I get the DPI
of
>> the
>> >> >> >> screen and use that as the pixels per inch and
>> then
>> >> >> >> compute the printed string size?

>> >> >> >.

>> >> >.

>> >.

>.

 
 
 

MeasureString results for printer GDI+

Post by Paul Hetheringto » Fri, 30 May 2003 07:02:10


You can try this.  It isn't pretty but should work.
Create a default font height (i.e. 10)
and in a loop do a measurestring on it to get the width.
If the width is > the pagewidth decrease the fontsize by some increment
(i.e. 0.25) and repeat the loop until the width desired is achieved
PsuedoCode Below

Fontsize=10
Do
    StringWidth=MeasureString(Fontsize,MyString)
    if StringWidth>PageWidth then
        FontSize =Fontsize - 0.25
    end if
Until StringWidth<PageWidth

cheers,
Paul


Quote:> The documentation for MeasureString states it returns the
> number of pixels the string will require.  How do I
> relate that to a printer?  If I consider it as DPI the
> measurement is not correct.  Do I get the DPI of the
> screen and use that as the pixels per inch and then
> compute the printed string size?

 
 
 

1. results of MeasureString are wrong

Dear all,

the following asserts, what is wrong ? (I use Windows XP)
Is there any work around ?

using (Font lFont = new Font("Courier New", 10))
{
    float a = e.Graphics.MeasureString("X",lFont).Width;
    float b = e.Graphics.MeasureString("XXXXX",lFont).Width;
    float c = e.Graphics.MeasureString("     ",lFont).Width; // 5 Blanks
    Debug.Assert(b/a==5);
    Debug.Assert(b==c);

2. How to update Uniface so that it recognises a new colum n added to a table

3. MeasureString BUG? - Measurestring.gif [0/1]

4. Code for GiF decoding...

5. Problems trying to print to a printer that is not a local printer

6. Intel 9600/EX and "vanilla" 1200s

7. WinOnCD3.5 problem on NT4

8. GDI+ performance (GDI+ 2.0 any time soon?)

9. Need help with binding SQL query results & then displaying those results.

10. Suggested DrawString and MeasureString overloads, and where do I submit it

11. problem with Graphics and measureString !

12. drawstring and measurestring and wrapping