0

I have a project in which I create an image with rotated text around an invisible circle.

The drawing in itself is working just fine. However, it seems that no matter the font I use, I always get the same result, which is I assume some low quality default font.

Here is the code :

        Bitmap objBmpImage = new Bitmap(1000, 1000);

        System.Drawing.Text.InstalledFontCollection installedFontCollection = new System.Drawing.Text.InstalledFontCollection();
        FontFamily[] fontFamilies = installedFontCollection.Families;

        System.Drawing.Font objFont = new System.Drawing.Font(fontFamilies.Where(x => x.Name == "Arial").FirstOrDefault(),10);

        Graphics objGraphics = Graphics.FromImage(objBmpImage);
        objGraphics.Clear(Color.Transparent);

        float angle = (float)360.0 / (float)competences.Count();

        objGraphics.TranslateTransform(500, 450);

        objGraphics.RotateTransform(-90 - (angle / 3));
        int nbComptetence = competences.Count();
        int indexCompetence = 0;
        foreach (T_Ref_Competence competence in competences)
        {
            byte r, g, b;
            HexToInt(competence.T_Ref_CompetenceNiveau2.T_Ref_CompetenceNiveau1.Couleur, out r, out g, out b);
            Brush brush = new System.Drawing.SolidBrush(Color.FromArgb(255,r,g,b));

            if (indexCompetence * 2 < nbComptetence)
            {
                objGraphics.DrawString(competence.Nom, objFont, brush, 255, 0);
                objGraphics.RotateTransform(angle);
            }
            else
            {
                objGraphics.RotateTransform(180);
                objGraphics.RotateTransform(angle/2);
                float textSize = objGraphics.MeasureString(competence.Nom, objFont).Width;
                objGraphics.DrawString(competence.Nom, objFont, brush, -253 - textSize, 0);
                objGraphics.RotateTransform(angle);
                objGraphics.RotateTransform(-180);
                objGraphics.RotateTransform(-angle / 2);

            }



            indexCompetence++;
        }

I get the font using the installed families like this

    System.Drawing.Text.InstalledFontCollection installedFontCollection = new System.Drawing.Text.InstalledFontCollection();
    FontFamily[] fontFamilies = installedFontCollection.Families;

    System.Drawing.Font objFont = new System.Drawing.Font(fontFamilies.Where(x => x.Name == "Arial").FirstOrDefault(),10);

I tried using other font but the result is always the same. Is there anything I am missing ? If not, what could be the reason ?

Thanks,

EDIT : To answer the question, what is it that I want exactly, consider this :

enter image description here

This image is a screenshot of a web site I am making. The chart in the middle was generated using charts.js, but its limitation force me to draw the text as a background image. It actually takes most of my screen so it can't really get much bigger than this. As you can see, the text font is pretty blurry and I would simply want it to be easier to read. I though the font was the problem, but I don't really know.

I am not really familiar with the whole image drawing part of C#, so if there are is better way to draw my text (which can change depending of many variables), I will gladly try other things.

David Brunelle
  • 6,528
  • 11
  • 64
  • 104

1 Answers1

2

Option 1: change text rendering

objGraphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel

Option 2: change the mode of anti aliasing

objGraphics.InterpolationMode=InterpolationMode.NearestNeighbor;

Option 3: change the DPI of the image

You'll get the best result if you scale the input image and then draw the text in higher DPI.

The default DPI for a Bitmap are 96. Probably the JS library exported with that setting.

If you want a smoother rendering of the font, you need to increase the DPI, e.g.

objBmpImage.SetResolution(1200,1200);

If you do so, you probably need to increase the number of pixels your Bitmap has.

If the "ugly" text just fitted the 1000x1000 picture, you now need 1000*1200/96=12500 pixels.

Before the change (using Arial 10 pt):

24 pixel wide J

After the change (still using Arial 10 pt):

180 pixel wide J

Note that the size in centimeters doesn't change. So it will still print well.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222