Good day! I am writing my own graphic editor. Now I am at the stage of developing a text tool. I use TextBox control to write text. And I ran into the following problem: when I redraw text from a textbox to a bitmap , it shifts a bit depending on the font. Here is the part of the code:
var g = Graphics.FromImage(bitmap); g.DrawString(textBox.Text, textBox.Font, new SolidBrush(textBox.ForeColor), textBox.Location); Can you tell me how to solve this problem? So that the text is drawn in the same place where it was in the textBox . I also tried to use a label and draw it on a bitmap , but the problem does not go away. All the same, a little, yes shifted.
I tried through Textrender.DrawText, but the text is still shifted. I will give an example for Label. I did everything in the PictureBox.OnPaint(object sender, PaintEventArgs e) method PictureBox.OnPaint(object sender, PaintEventArgs e) . Below are two pieces of code:
one)
label.Visible = false; TextRenderer.DrawText(e.Graphics, label.Text, label.Font, label.Location, label.ForeColor); 2)
label.Visible = false; var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height); var g = Graphics.FromImage(bitmap); TextRenderer.DrawText(g, label.Text, label.Font, label.Location, label.ForeColor); e.Graphics.DrawImage(bitmap, 0, 0); Those. in the first case I draw on the pictureBox itself, in the second - on a bitmap, which I then draw on the pictureBox.
I can notice that the line:
label.UseCompatibleTextRendering = true; you can not write, because that assign true, that false - the result is the same in my case (although it is not clear why)
label.Visible = false; - wrote to ensure that it does not appear on the pictureBox when we have already drawn the text.
Below I put three pictures:
1 - initially looks like a label on the pictureBox,
2 - as when drawing on the pictureBox,
3 - as when drawing on bitmap.
They clearly show that the text is moving. Moreover, in the case of bitmap, the font also became bold for some reason.

TextRenderer.DrawText- Alexander Petrov