Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 32297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:44:26+00:00 2026-05-10T13:44:26+00:00

I’m using the .NETCF (Windows Mobile) Graphics class and the DrawString() method to render

  • 0

I’m using the .NETCF (Windows Mobile) Graphics class and the DrawString() method to render a single character to the screen.

The problem is that I can’t seem to get it centred properly. No matter what I set for the Y coordinate of the location of the string render, it always comes out lower than that and the larger the text size the greater the Y offset.

For example, at text size 12, the offset is about 4, but at 32 the offset is about 10.

I want the character to vertically take up most of the rectangle it’s being drawn in and be centred horizontally. Here’s my basic code. this is referencing the user control it’s being drawn in.

Graphics g = this.CreateGraphics();  float padx = ((float)this.Size.Width) * (0.05F); float pady = ((float)this.Size.Height) * (0.05F);  float width = ((float)this.Size.Width) - 2 * padx; float height = ((float)this.Size.Height) - 2 * pady;  float emSize = height;  g.DrawString(letter, new Font(FontFamily.GenericSansSerif, emSize, FontStyle.Regular),             new SolidBrush(Color.Black), padx, pady); 

Yes, I know there is the label control that I could use instead and set the centring with that, but I actually do need to do this manually with the Graphics class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T13:44:27+00:00Added an answer on May 10, 2026 at 1:44 pm

    Through a combination of the suggestions I got, I came up with this:

        private void DrawLetter()     {         Graphics g = this.CreateGraphics();          float width = ((float)this.ClientRectangle.Width);         float height = ((float)this.ClientRectangle.Width);          float emSize = height;          Font font = new Font(FontFamily.GenericSansSerif, emSize, FontStyle.Regular);          font = FindBestFitFont(g, letter.ToString(), font, this.ClientRectangle.Size);          SizeF size = g.MeasureString(letter.ToString(), font);         g.DrawString(letter, font, new SolidBrush(Color.Black), (width-size.Width)/2, 0);      }      private Font FindBestFitFont(Graphics g, String text, Font font, Size proposedSize)     {         // Compute actual size, shrink if needed         while (true)         {             SizeF size = g.MeasureString(text, font);              // It fits, back out             if (size.Height <= proposedSize.Height &&                  size.Width <= proposedSize.Width) { return font; }              // Try a smaller font (90% of old size)             Font oldFont = font;             font = new Font(font.Name, (float)(font.Size * .9), font.Style);             oldFont.Dispose();         }     } 

    So far, this works flawlessly.

    The only thing I would change is to move the FindBestFitFont() call to the OnResize() event so that I’m not calling it every time I draw a letter. It only needs to be called when the control size changes. I just included it in the function for completeness.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 109k
  • Answers 109k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use sites application: Site.objects.get_current().domain Example direct from documentation: from django.contrib.sites.models… May 11, 2026 at 9:27 pm
  • Editorial Team
    Editorial Team added an answer When using iplGetRotateShift you need to specify the center of… May 11, 2026 at 9:27 pm
  • Editorial Team
    Editorial Team added an answer Your domain object can have events that notify the outside… May 11, 2026 at 9:27 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.