By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,221 Members | 1,663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,221 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
If this is an offline process, testing each column of pixels should be
enough.

You could also try
http://msdn.microsoft.com/library/de...ntext_52at.asp
and in particular the GLYPHMETRICS structure that should allow to get the
"box" in which the character is drawn...

Patrice
--

"Nathan Sokalski" <nj********@hotmail.com> a écrit dans le message de
news:OC****************@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
nj********@hotmail.com
http://www.nathansokalski.com/

Nov 21 '05 #2

P: n/a
Do you know of anywhere where I can see an example? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Patrice" <no****@nowhere.com> wrote in message
news:Oc**************@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/de...ntext_52at.asp
and in particular the GLYPHMETRICS structure that should allow to get the
"box" in which the character is drawn...

Patrice
--

"Nathan Sokalski" <nj********@hotmail.com> a écrit dans le message de
news:OC****************@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
nj********@hotmail.com
http://www.nathansokalski.com/


Nov 21 '05 #3

P: n/a
Try around :
http://cvs.sourceforge.net/viewcvs.p...rm1.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

"Nathan Sokalski" <nj********@hotmail.com> a écrit dans le message de
news:%2****************@TK2MSFTNGP10.phx.gbl...
Do you know of anywhere where I can see an example? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Patrice" <no****@nowhere.com> wrote in message
news:Oc**************@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/de...ntext_52at.asp and in particular the GLYPHMETRICS structure that should allow to get the "box" in which the character is drawn...

Patrice
--

"Nathan Sokalski" <nj********@hotmail.com> a écrit dans le message de
news:OC****************@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
nj********@hotmail.com
http://www.nathansokalski.com/



Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.