0
  Bitmap bmpChar = new Bitmap(16,16);       
  FontFamily fontFamily = new FontFamily("Arial");

  Font font = new Font(
               fontFamily,
               16,
               FontStyle.Regular,
               GraphicsUnit.Pixel);


  Graphics g = Graphics.FromImage(testBmp);
  g.DrawString("test", font, Brushes.Red, 0, 0);

Upper code prints two characters in 16x16 area(it has "te" of "test" while I expected only a "t"). Whats could be a platform-independent(32-bit, 64bit, NT, XP, 7, 10) way to have constant width characters for all letters and numbers when drawing them as strings onto a bitmap, in winforms?

huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97

1 Answers1

1

I would use a monospaced font if it's possible. Otherwise you'd just be either stretching the letters out after rasterization or having to calculate the amount of spacing to put in between each letter which would be a lot more complicated.

ameer
  • 2,598
  • 2
  • 21
  • 36
  • Tested with generic monospace but with 16px again, it rendered t and half of e in 16x16 area. Maybe monospace can't be at certain resolutions? Ok, using 24 as size solved it, with generic monospace. – huseyin tugrul buyukisik Dec 09 '17 at 20:39