private void Form1_Load(object sender, EventArgs e) { // create a Graphics object Graphics g = this.CreateGraphics(); // draw a rectangle g.DrawRectangle(Pens.Black, 50, 50, 100, 100); // dispose of the Graphics object g.Dispose(); }
private void Form1_Load(object sender, EventArgs e) { // create a Graphics object Graphics g = this.CreateGraphics(); // draw text g.DrawString("Hello World", this.Font, Brushes.Black, 50, 50); // dispose of the Graphics object g.Dispose(); }This code example demonstrates how to use the CreateGraphics method to create a Graphics object, and then draw text on the form using the DrawString method of the Graphics object. The Brushes.Black parameter sets the color of the text. Package library: System.Windows.Forms