ASP.NET TextRenderer.DrawText Horrible Text Images

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:

enter image description here

And here is what it looks like:

enter image description here

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:

enter image description here

This is the result with Graphics.DrawString()

enter image description here

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:

enter image description here

This is the result of drawing:

enter image description here

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:

enter image description here

Similarly, thanks for the suggestions and answers.

+7
source share
4 answers

I created something to create buttons with images using similar functions, and I had problems with kerneling and a font that didn't stretch to what I wanted. The following settings made me really close to what I wanted, but still not 100%.

 objGraphics2.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; objGraphics2.TextRenderingHint = System.Drawing.Drawing2D.TextRenderingHint.AntiAliasGridFit; 
+2
source

I'm not sure if this will help, but why not create a font without calling a function, for example:

  Font textFont = new Font(textFontFamily, textSize, GraphicsUnit.Pixel); 
+3
source

Set Graphics.TextRenderingHint to SingleBitPerPixelGridFit .

+2
source

I'm not sure if this will solve the problem, but I had a similar problem with drawing text in Direct3D, check PixelOffsetMode , set it to Half .

0
source

All Articles