System.Drawing

.NET Framework Class Library

Introduction

The System.Drawing namespace provides access to basic graphics functionality. It enables you to draw shapes, text, and images on the screen or to files. This namespace contains classes for working with graphics contexts, bitmaps, metafiles, fonts, and colors.

Key Concepts

Classes

Example Usage

Here's a simple example of how to draw a red line on a control using the System.Drawing namespace:


using System;
using System.Drawing;
using System.Windows.Forms;

public class DrawingForm : Form
{
    public DrawingForm()
    {
        this.Paint += new PaintEventHandler(DrawingForm_Paint);
        this.Size = new Size(400, 300);
        this.Text = "System.Drawing Example";
    }

    private void DrawingForm_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;

        // Create a red pen with a thickness of 2 pixels
        using (Pen redPen = new Pen(Color.Red, 2))
        {
            // Define the start and end points of the line
            Point startPoint = new Point(50, 50);
            Point endPoint = new Point(300, 150);

            // Draw the line
            g.DrawLine(redPen, startPoint, endPoint);
        }

        // Draw some text
        using (Font drawFont = new Font("Arial", 16))
        using (SolidBrush drawBrush = new SolidBrush(Color.Blue))
        {
            g.DrawString("Hello, System.Drawing!", drawFont, drawBrush, new Point(50, 200));
        }
    }

    [STAThread]
    public static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new DrawingForm());
    }
}
        

Related Topics