0

Background: I am working on a legacy map application using WinForms and .NET4.0. The labels, roads, icons are painted to a separate bitmap called "overlay" apart from the terrain. The background of the overlay is transparent and later will be painted over the terrain bitmap.

The problem is that while painting the text on the bitmap, the text style is being influenced by bitmap background. I show you an example picture of the overlay:

enter image description here

Take a look at the label "abbey" and "white". "White" has been painted on completely transparent background and now looks like if it were written in bold. While "ab" in the "abbey" looks regular text as it has been painted over the icon.

Here is the code that produces the label:

var bmp = new Bitmap(mapControl.Width, mapControl.Height);
using (var graphics = Graphics.FromImage(bmp))
{
    var labelRectangle = GetLabelBorders();
    Color labelBackgroundColor = Color.FromArgb(128, Color.FromName("LightPink"));
    SolidBrush sb = new SolidBrush(labelBackgroundColor);
    graphics.FillRectangle(sb, labelRectangle );
    graphics.DrawRectangle(WhitePen, labelRectangle .X, labelRectangle .Y, 
                 labelRectangle .Width - 1, labelRectangle .Height - 1);
    Font timesnew8 = new Font("TimesNew", 8);
    StringFormat strForm = new StringFormat();
    strForm.Alignment = StringAlignment.Near;
    graphics.DrawString("abbey", timesnew8, Brushes.Black, labelRectangle , strForm);
}

I want the text not-bold looking. How can I do that?

user256890
  • 3,396
  • 5
  • 28
  • 45
  • 1
    Have you tried using a smoothingmode to antialias the text? That or a TextRenderingHint? http://msdn.microsoft.com/en-us/library/9t6sa8s9.aspx or http://msdn.microsoft.com/en-us/library/system.drawing.graphics.textrenderinghint.aspx – Tom Jul 04 '12 at 10:28
  • Where are you painting from? Is doublebuffering enabled? – leppie Jul 04 '12 at 10:28
  • @leppie: I updated the code to reflect more the reality. No dubble buffering, I am painting into a local Bitmap object. – user256890 Jul 04 '12 at 10:38
  • @user256890: Try `graphics.Clear(labelBackgroundColor)` after creating it. You can probably drop the `FillRectangle` call to. – leppie Jul 04 '12 at 10:41
  • 1
    @Tom: TextRenderingHint with setting System.Drawing.Text.TextRenderingHint.AntiAliasGridFit works great. If you make it an answer, I will accept it. – user256890 Jul 04 '12 at 10:57
  • possible duplicate of [Merging graphics objects does not render text correctly](http://stackoverflow.com/questions/9681852/merging-graphics-objects-does-not-render-text-correctly) – Hans Passant Jul 04 '12 at 12:36

1 Answers1

2

As comment earlier says: try using the TextRenderingHint property on your Graphics object to see if it renders the text differently.

Tom
  • 3,354
  • 1
  • 22
  • 25