Home All Groups Group Topic Archive Search About

Finding the leftmost pixel in a piece of text when using GDI+

Author
28 Jun 2005 4:11 AM
Nathan Sokalski
I am using GDI+ to generate buttons in my application. Because I want the
left and right edges of the buttons to be a certain number of pixels from
the start and end of the text, I need to know (or be able to calculate) the
left and rightmost pixels of the text. However, this is a problem because
not all letters have their leftmost pixel at the same X position. For
example, when using the following font:

Dim buttonfont As New Font("Arial", 19, FontStyle.Bold, GraphicsUnit.Point)


and using the following lines to draw text:

For i As Integer = 65 To 90

testgraphics.DrawString(Chr(i), buttonfont, Brushes.White, 0, 35 + (i - 64)
* 20)

Next


The characters 'A' and 'J' start 2 pixels farther to the left than most
other characters (there are also a couple other characters that are offset
by 1 or 2 pixels). Is there any easy way to determine what X position the
first and last pixel of a character is at? Because this depends on the font,
the only way to do it manually would be to test each pixel. Is there a
method that can be used to do this? Thanks.
--
Nathan Sokalski
njsokal***@hotmail.com
http://www.nathansokalski.com/

Author
28 Jun 2005 8:28 AM
Patrice
If this is an offline process, testing each column of pixels should be
enough.

You could also try
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fontext_52at.asp
and in particular the GLYPHMETRICS structure that should allow to get the
"box" in which the character is drawn...

Patrice
--

Show quoteHide quote
"Nathan Sokalski" <njsokal***@hotmail.com> a écrit dans le message de
news:OCD%23td5eFHA.2888@TK2MSFTNGP15.phx.gbl...
> I am using GDI+ to generate buttons in my application. Because I want the
> left and right edges of the buttons to be a certain number of pixels from
> the start and end of the text, I need to know (or be able to calculate)
the
> left and rightmost pixels of the text. However, this is a problem because
> not all letters have their leftmost pixel at the same X position. For
> example, when using the following font:
>
> Dim buttonfont As New Font("Arial", 19, FontStyle.Bold,
GraphicsUnit.Point)
>
>
> and using the following lines to draw text:
>
> For i As Integer = 65 To 90
>
> testgraphics.DrawString(Chr(i), buttonfont, Brushes.White, 0, 35 + (i -
64)
> * 20)
>
> Next
>
>
> The characters 'A' and 'J' start 2 pixels farther to the left than most
> other characters (there are also a couple other characters that are offset
> by 1 or 2 pixels). Is there any easy way to determine what X position the
> first and last pixel of a character is at? Because this depends on the
font,
> the only way to do it manually would be to test each pixel. Is there a
> method that can be used to do this? Thanks.
> --
> Nathan Sokalski
> njsokal***@hotmail.com
> http://www.nathansokalski.com/
>
>
Author
28 Jun 2005 11:00 PM
Nathan Sokalski
Do you know of anywhere where I can see an example? Thanks.
--
Nathan Sokalski
njsokal***@hotmail.com
http://www.nathansokalski.com/

Show quoteHide quote
"Patrice" <nob***@nowhere.com> wrote in message
news:OcA9bt7eFHA.3712@TK2MSFTNGP09.phx.gbl...
> If this is an offline process, testing each column of pixels should be
> enough.
>
> You could also try
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fontext_52at.asp
> and in particular the GLYPHMETRICS structure that should allow to get the
> "box" in which the character is drawn...
>
> Patrice
> --
>
> "Nathan Sokalski" <njsokal***@hotmail.com> a écrit dans le message de
> news:OCD%23td5eFHA.2888@TK2MSFTNGP15.phx.gbl...
>> I am using GDI+ to generate buttons in my application. Because I want the
>> left and right edges of the buttons to be a certain number of pixels from
>> the start and end of the text, I need to know (or be able to calculate)
> the
>> left and rightmost pixels of the text. However, this is a problem because
>> not all letters have their leftmost pixel at the same X position. For
>> example, when using the following font:
>>
>> Dim buttonfont As New Font("Arial", 19, FontStyle.Bold,
> GraphicsUnit.Point)
>>
>>
>> and using the following lines to draw text:
>>
>> For i As Integer = 65 To 90
>>
>> testgraphics.DrawString(Chr(i), buttonfont, Brushes.White, 0, 35 + (i -
> 64)
>> * 20)
>>
>> Next
>>
>>
>> The characters 'A' and 'J' start 2 pixels farther to the left than most
>> other characters (there are also a couple other characters that are
>> offset
>> by 1 or 2 pixels). Is there any easy way to determine what X position the
>> first and last pixel of a character is at? Because this depends on the
> font,
>> the only way to do it manually would be to test each pixel. Is there a
>> method that can be used to do this? Thanks.
>> --
>> Nathan Sokalski
>> njsokal***@hotmail.com
>> http://www.nathansokalski.com/
>>
>>
>
>
Author
29 Jun 2005 9:19 AM
Patrice
Try around :
http://cvs.sourceforge.net/viewcvs.py/numerator/test/FontTester/Form1.cs?rev=1.2

I've done a search using gmBlackBoxX (you could use any other name that
belongs to this structure) on Google...

Good luck.

--
Patrice

Show quoteHide quote
"Nathan Sokalski" <njsokal***@hotmail.com> a écrit dans le message de
news:%23lFRkUDfFHA.3528@TK2MSFTNGP10.phx.gbl...
> Do you know of anywhere where I can see an example? Thanks.
> --
> Nathan Sokalski
> njsokal***@hotmail.com
> http://www.nathansokalski.com/
>
> "Patrice" <nob***@nowhere.com> wrote in message
> news:OcA9bt7eFHA.3712@TK2MSFTNGP09.phx.gbl...
> > If this is an offline process, testing each column of pixels should be
> > enough.
> >
> > You could also try
> >
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fontext_52at.asp
Show quoteHide quote
> > and in particular the GLYPHMETRICS structure that should allow to get
the
> > "box" in which the character is drawn...
> >
> > Patrice
> > --
> >
> > "Nathan Sokalski" <njsokal***@hotmail.com> a écrit dans le message de
> > news:OCD%23td5eFHA.2888@TK2MSFTNGP15.phx.gbl...
> >> I am using GDI+ to generate buttons in my application. Because I want
the
> >> left and right edges of the buttons to be a certain number of pixels
from
> >> the start and end of the text, I need to know (or be able to calculate)
> > the
> >> left and rightmost pixels of the text. However, this is a problem
because
> >> not all letters have their leftmost pixel at the same X position. For
> >> example, when using the following font:
> >>
> >> Dim buttonfont As New Font("Arial", 19, FontStyle.Bold,
> > GraphicsUnit.Point)
> >>
> >>
> >> and using the following lines to draw text:
> >>
> >> For i As Integer = 65 To 90
> >>
> >> testgraphics.DrawString(Chr(i), buttonfont, Brushes.White, 0, 35 + (i -
> > 64)
> >> * 20)
> >>
> >> Next
> >>
> >>
> >> The characters 'A' and 'J' start 2 pixels farther to the left than most
> >> other characters (there are also a couple other characters that are
> >> offset
> >> by 1 or 2 pixels). Is there any easy way to determine what X position
the
> >> first and last pixel of a character is at? Because this depends on the
> > font,
> >> the only way to do it manually would be to test each pixel. Is there a
> >> method that can be used to do this? Thanks.
> >> --
> >> Nathan Sokalski
> >> njsokal***@hotmail.com
> >> http://www.nathansokalski.com/
> >>
> >>
> >
> >
>
>