I am trying to write a simple piece of text into a bitmap, using the System.Drawing.Graphics class.
When creating the Graphics class from an existing bitmap, using Graphics.FromImage(MyBitmap), the text being written looks like it's very bold (First line in picture below).
When drawing the same piece of text using a Graphics object that isn't created using the Graphics.FromImage method, the text looks as intended (Second line in picture below).
Using the debugger to examing the two different Graphics objects, reveals that their properties are identical, which makes me wonder why the Graphics.DrawText method creates two completely different results.
The code used to generate the screenshot above, looks like this:
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
string text = "1.000";
Rectangle rectangle = new Rectangle(0, 0, 200, 200);
System.Drawing.StringFormat stringFormat = new System.Drawing.StringFormat(System.Drawing.StringFormatFlags.NoWrap);
using(System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(e.ClipRectangle.Width, e.ClipRectangle.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
using(System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap))
using(System.Drawing.Font font = new System.Drawing.Font("Tahoma", 10))
using(System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black))
{
// Text looks very bold.
graphics.DrawString(text, font, drawBrush, rectangle, stringFormat);
e.Graphics.DrawImage(bitmap, new Point(0, 0));
// Text looks normal.
e.Graphics.DrawString(text, font, drawBrush, new Rectangle(0, 20, 200, 200), stringFormat);
}
}
}
I am attempting to write the non-bold piece of text into a bitmap, such that I can use it elsewhere in my program.
Can anyone explain why the Graphics.DrawText method produces two different results, in the example code above, and how I can make the text output for the first Graphics object appear identical to the second Graphics object?
Try
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
Or any of the other TextRenderingHints
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.