Since I tried to draw a line with each combination of anti-aliasing and rendering using Graphics.DrawString()
, I thought text rendering would do a better job drawing my lines, but I think this is wrong.
Here's how it should look:

And here is what it looks like:

Here is my code:
Graphics objGraphics2 = Graphics.FromImage(objBitmap); objGraphics2.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; objGraphics2.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; objGraphics2.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; objGraphics2.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear; Font textFont = new Font(textFontFamily, PxtoEm(textSize)); SolidBrush b = new SolidBrush(textColor); TextRenderer.DrawText(objGraphics2, textValue, textFont, new Rectangle(0, 0, Width, Height), textColor);
Is my PxtoEm
method wrong?
public float PxtoEm(int px) { float em = (float)(Convert.ToDouble(Convert.ToDouble(px) * Convert.ToDouble(72) / Convert.ToDouble(objBitmap.HorizontalResolution))); return em; }
I need some suggestions because it is really terrible, getting worse with large fonts, and the images are not shrinking.
UPDATE: it works with large fonts (e.g. 20px), but with smaller fonts it is typed in some letters:
Thus, it should be in Arial 10px font:

This is the result with Graphics.DrawString()

As you can see, this is really not very good, but the closest I got. I made some changes to the code and got the best results with a large font:
So it supposedly has the font Arial 20px:

This is the result of drawing:

And here is the modified code (I chose the em method and used the pixels directly, switched to Graphics.DrawString () instead of TextRenderer.DrawText ()
Graphics objGraphics = Graphics.FromImage(objBitmap); objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; objGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; objGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; objGraphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; Font textFont = new Font(textFontFamily, textSize,GraphicsUnit.Pixel); SolidBrush b = new SolidBrush(textColor); PointF origin = new PointF((float)TextLeft,(float)TextTop); StringFormat format = StringFormat.GenericTypographic; objGraphics.DrawString(textValue, textFont, b , origin, format);
If anyone has a suggestion, maybe write a different method for a smaller text size and use the code above for a larger one, since it works beautifully, post it and I will try!
UPDATE 3: Finally found a solution for everything, and the solution was pretty simple: DO NOT USE A TRANSPARENT BACKGROUND!
And settings:
objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; // <-- important! objGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; objGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; objGraphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; objGraphics.TextContrast = 0;
Here is the final image with these settings on a white background:

Similarly, thanks for the suggestions and answers.