Mega Code Archive

 
Categories / C# / 2D Graphics
 

Draw Rectangle surrounding the Text

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Text; using System.Windows.Forms; public class Form1 : Form {     protected override void OnPaint(PaintEventArgs e) {     Graphics g = e.Graphics;     g.PageUnit = GraphicsUnit.Inch;     Pen p = new Pen(Color.Black, 1 / 96f);     Font f = new Font("Times New Roman", 16);     String s = "Abc";     SizeF sf = g.MeasureString(s, f);     g.DrawRectangle(p, 1, 1, sf.Width, sf.Height);     g.DrawString(s, f, Brushes.Black, 1, 1);     f.Dispose();     p.Dispose();     }     public static void Main() {         Application.Run(new Form1());     } }