Drawing with C#

A tutorial with examples of how to draw different things on a form using OnPaint.

In this tutorial We’re going to play a little by drawing with C#.

Start a new Windows Application project. Leave the form’s width and height unchanged, Visual C# .NET default (300 x 300 pixels).
We override OnPaint with the following code:

protected override void OnPaint(PaintEventArgs paintEvnt)
{
    // Get the graphics object
    Graphics gfx = paintEvnt.Graphics;
    // Create a new pen that we shall use for drawing the line
    Pen myPen = new Pen(Color.Black);
    // Loop and create a new line 10 pixels below the last one
    for(int i = 20; i < 250; i = i + 10)
    {
    gfx.DrawLine(myPen, 20, i, 270, i);
    }
}

The result is this:

Play with the code further and you’ll see you can make some nice little squares making the form look like a checkerboard.
This can be done with the following code:

protected override void OnPaint(PaintEventArgs paintEvnt)
        {
            // Get the graphics object
            Graphics gfx = paintEvnt.Graphics;
            // Create a new pen that we shall use for drawing the line
            Pen myPen = new Pen(Color.Black);
            // Loop and create a horizontal line 10 pixels below the last one
            for(int i = 20; i <= 250; i = i + 10)
            {
                gfx.DrawLine(myPen, 20, i, 270, i);
            }
            // Loop and create a vertical line 10 pixels next to the last one
            for(int x = 20; x < 280; x = x + 10)
            {
                gfx.DrawLine(myPen, x, 20, x, 250);
            }
        }

Same as the first piece of code, just that now we also loop and create a vertical line 10 pixels next to the last one.

Rectangles

Try this code:

protected override void OnPaint(PaintEventArgs paintEvnt)
        {
            // Get the graphics object
            Graphics gfx = paintEvnt.Graphics;
            // Create a new pen that we shall use for drawing the line
            Pen myPen = new Pen(Color.Black);
            // Loop until the coordinates reach 250 (the lower right corner of the form)
            for(int i = 0; i < 250; i = i + 50)
            {
                // Draw a 50x50 pixels rectangle
                gfx.DrawRectangle(myPen, i, i, 50, 50);
            }
        }

Let’s move to something more serious.

protected override void OnPaint(PaintEventArgs paintEvnt)
{
    // Get the graphics object
    Graphics gfx = paintEvnt.Graphics;
    int x1 = 0;
    int y1 = 0;
    // Loop trough the 255 values red can have
    for(int i = 0; i <= 255; i++)
    {
        // Create new brush with a defined color
        Color brushColor = Color.FromArgb(i, 0, 0);
        // The brush is solid because we want a solid rectangle
        SolidBrush myBrush = new SolidBrush(brushColor);
        // Actually draw the rectangle
        gfx.FillRectangle(myBrush, x1, y1, 10, 10);
        // The next rectangle should be near the last one
        x1 = x1 + 10;
        // If the row is complete start another one
        if((x1 % 290) == 0)
        {
            y1 = y1 + 10;
            x1 = 0;
        }
        }
        for(int i = 0; i <= 255; i++)
        {
        Color brushColor = Color.FromArgb(0, i, 0);
        SolidBrush myBrush = new SolidBrush(brushColor);
        gfx.FillRectangle(myBrush, x1, y1, 10, 10);
        x1 = x1 + 10;
        if((x1 % 290) == 0)
        {
            y1 = y1 + 10;
            x1 = 0;
        }
    }
    for(int i = 0; i <= 255; i++)
    {
    Color brushColor = Color.FromArgb(0, 0, i);
    SolidBrush myBrush = new SolidBrush(brushColor);
    gfx.FillRectangle(myBrush, x1, y1, 10, 10);
    x1 = x1 + 10;
    if((x1 % 290) == 0)
    {
        y1 = y1 + 10;
        x1 = 0;
    }
}

The above code produces the following:

The easiest way to learn is by examples and exercises. Therefore search for different results and try to accomplish something else by modifying the pieces of code or creating your own.

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top