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

Discussion in 'ASP .Net' started by Nathan Sokalski, Jun 28, 2005.

  1. 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

    http://www.nathansokalski.com/
    Nathan Sokalski, Jun 28, 2005
    #1
    1. Advertising

  2. Nathan Sokalski

    Patrice Guest

    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" <> a écrit dans le message de
    news:OCD%...
    > 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
    >
    > http://www.nathansokalski.com/
    >
    >
    Patrice, Jun 28, 2005
    #2
    1. Advertising

  3. Do you know of anywhere where I can see an example? Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Patrice" <> wrote in message
    news:...
    > 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" <> a écrit dans le message de
    > news:OCD%...
    >> 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
    >>
    >> http://www.nathansokalski.com/
    >>
    >>

    >
    >
    Nathan Sokalski, Jun 29, 2005
    #3
  4. Nathan Sokalski

    Patrice Guest

    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

    "Nathan Sokalski" <> a écrit dans le message de
    news:%...
    > Do you know of anywhere where I can see an example? Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    > "Patrice" <> wrote in message
    > news:...
    > > 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" <> a écrit dans le message de
    > > news:OCD%...
    > >> 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
    > >>
    > >> http://www.nathansokalski.com/
    > >>
    > >>

    > >
    > >

    >
    >
    Patrice, Jun 29, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?cGJ1bQ==?=

    GDI+ - limits for pixel dimensions in a bitmap object

    =?Utf-8?B?cGJ1bQ==?=, Jun 18, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    346
    =?Utf-8?B?cGJ1bQ==?=
    Jun 18, 2004
  2. Sinora
    Replies:
    1
    Views:
    821
    David White
    Nov 11, 2003
  3. Robin van de Water [synthesiSFactory]

    GDI+ is not properly initialized (internal GDI+ error).

    Robin van de Water [synthesiSFactory], Jan 10, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    1,326
    Robin van de Water [synthesiSFactory]
    Jan 10, 2007
  4. Nathan Sokalski

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

    Nathan Sokalski, Jun 28, 2005, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    145
    Patrice
    Jun 29, 2005
  5. Nathan Sokalski

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

    Nathan Sokalski, Jun 28, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    125
    Patrice
    Jun 29, 2005
Loading...

Share This Page