System.Drawing.Graphics
lastmod 2026-11-10created 2017-01-172 edits
System.Drawing.Graphics is a class in .NET that represents a GDI+-based 2D drawing surface. It provides methods such as DrawLine, DrawImage, and DrawString for drawing lines, shapes, images, and strings, and you draw by obtaining a Graphics object from a form or bitmap. A typical use is to compute the size a string will occupy with MeasureString before drawing it, then position it accordingly.
MeasureString
using (Font bigFont = new Font(SystemFonts.DefaultFont.FontFamily, 14, FontStyle.Regular)) {
SizeF size = g.MeasureString(texttodraw, bigFont, 14);
g.DrawString(texttodraw, bigFont, Brushes.Red, pictureBox1.Location.X + (pictureBox1.Width / 2) - (size.Width / 2), pictureBox1.Location.Y - 30);
}
Favorite site
- [https://msdn.microsoft.com/en-us/library/system.drawing.graphics(v=vs.110.aspx MSDN - Graphics Class]
- stackoverflow - c# write text on bitmap