Tech Off Thread

4 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

DrawString emsize and centered glyph.

Back to Forum: Tech Off
  • User profile image

    Still scratching my head; drawing text using C#

    The objective : scale a character constrained in a rectangle as a glyph.

    But I want it to be filling a rectangle as much as possible. Text portion of the character should be centered in that rectangle.

    The objective is to have a scalable control that scales as smootly as possible with its contained text when you resize it.

    Let's call it the "Smooth Vector Scale Control".

    So far I am able to guess the right emsize for the rectangle, the screen dpi, for a specific font. I derive emsize using a zoom factor until one of the edges of the character reaches the rectangle edges.

    But centering it is my main problem now as drawString will not draw starting a 0,0 but with an offset for character spacing all around the character being print. Which is desireable when printing strings.
    ( You don't want that underscore positioned at 0,0 ).
    But this is it. I want my underscore positioned at 0.0 or at least to know its bounds in the layout.

    This is a way out: 
    Derive the printable content (ink) only.
    Or Retreive the bounds (size and the position) for the smallest rectangle containing all the printable ink content.
    With that I could be centering my text properly.

    Is there another idea out there?

    Requirements :
    - Any character of any font.
    - Scales with rectangle 
      (proportionally until one edge reached)
    - Any DPI
    - .NET framework, C#

  • User profile image

    Have you considered constructing a Metafile of your text and/or graphics, retrieving the dimensions, setting a GDI+ transformation, and then "playing" the Metafile to the screen?

    Admittedly, System.Drawing.Imaging.Metafile isn't the most documented class, but it sounds like it should be right up your alley.

  • User profile image

    I will defenitely look at it now.

  • User profile image
    Tom Servo

    You wouldn't want to know the headaches I had with DrawString while writing a custom text viewer for my IRC client, all just to get proportional characters to line up correctly.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.