MSDN Documentation – Drawing Primitives

Overview

The Drawing Primitives API provides a set of methods for rendering basic geometric shapes such as lines, rectangles, ellipses, and polygons using the Graphics object. These primitives are the building blocks for custom UI components, charting libraries, and game graphics on Windows.

Common Classes

Lines

Draw straight line segments using Graphics.DrawLine.

// C# example
using System.Drawing;

void DrawSampleLine(Graphics g)
{
    using (Pen pen = new Pen(Color.Blue, 2))
    {
        g.DrawLine(pen, new Point(20, 30), new Point(200, 150));
    }
}

Rectangles

Render both outlined and filled rectangles.

// C++/CLI example
using namespace System::Drawing;

void DrawRectangles(Graphics^ g)
{
    Pen^ outline = gcnew Pen(Color::DarkRed, 3);
    Brush^ fill   = gcnew SolidBrush(Color::FromArgb(128, 255, 200, 0));

    // Outline
    g->DrawRectangle(outline, 30, 40, 120, 80);
    // Filled
    g->FillRectangle(fill, 180, 40, 120, 80);
}

Ellipses & Circles

Use Graphics.DrawEllipse and Graphics.FillEllipse for ovals and circles.

// C# example
using System.Drawing;

void DrawEllipses(Graphics g)
{
    Pen pen = new Pen(Color.Green, 2);
    Brush brush = new SolidBrush(Color.FromArgb(64, Color.Green));

    // Outline only
    g.DrawEllipse(pen, 50, 150, 100, 60);
    // Filled ellipse
    g.FillEllipse(brush, 200, 150, 100, 60);
}

Polygons

Draw complex shapes with an array of Point structures.

// C# example
using System.Drawing;

void DrawPolygon(Graphics g)
{
    Point[] pts = {
        new Point(100, 300),
        new Point(150, 250),
        new Point(200, 300),
        new Point(175, 350),
        new Point(125, 350)
    };

    using (Pen pen = new Pen(Color.Purple, 2))
    using (Brush brush = new SolidBrush(Color.FromArgb(80, Color.Purple)))
    {
        g.FillPolygon(brush, pts);
        g.DrawPolygon(pen, pts);
    }
}

Advanced Tips