Windows Forms
C# programming
pixel drawing
graphics programming
.NET development

Draw a single pixel on Windows Forms

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Drawing one pixel in Windows Forms is easy in principle, but the correct technique depends on whether you need the pixel only for the current paint pass or as persistent image data. The wrong approach often appears to work for a moment and then disappears as soon as the form repaints.

The Simple Rule

If you only want to render a pixel during painting, draw it inside the Paint event. If you want the pixel to remain part of an image, draw it onto a Bitmap and then paint that bitmap.

That distinction matters because Windows Forms is repaint-driven. The screen can be redrawn at any time when the window is uncovered, resized, or invalidated.

Option 1: Draw During the Paint Event

For a single visible pixel, the simplest approach is to paint a 1 x 1 rectangle.

csharp
1using System;
2using System.Drawing;
3using System.Windows.Forms;
4
5public class PixelForm : Form
6{
7    public PixelForm()
8    {
9        Text = "Single Pixel";
10        ClientSize = new Size(200, 120);
11        Paint += PixelForm_Paint;
12    }
13
14    private void PixelForm_Paint(object sender, PaintEventArgs e)
15    {
16        e.Graphics.FillRectangle(Brushes.Red, 50, 40, 1, 1);
17    }
18
19    [STAThread]
20    public static void Main()
21    {
22        Application.EnableVisualStyles();
23        Application.Run(new PixelForm());
24    }
25}

This is fully runnable. Every time the form repaints, Windows Forms redraws the red pixel at coordinate 50, 40.

Using FillRectangle is clearer than trying to fake a pixel with DrawLine. A one-pixel rectangle expresses the intent directly.

Option 2: Draw Onto a Bitmap

If you want the pixel to persist as part of an image that can be updated over time, use a backing bitmap.

csharp
1using System;
2using System.Drawing;
3using System.Windows.Forms;
4
5public class BitmapPixelForm : Form
6{
7    private readonly Bitmap canvas = new Bitmap(200, 120);
8
9    public BitmapPixelForm()
10    {
11        Text = "Bitmap Pixel";
12        ClientSize = new Size(200, 120);
13        Paint += BitmapPixelForm_Paint;
14
15        canvas.SetPixel(50, 40, Color.Blue);
16    }
17
18    private void BitmapPixelForm_Paint(object sender, PaintEventArgs e)
19    {
20        e.Graphics.DrawImageUnscaled(canvas, 0, 0);
21    }
22
23    [STAThread]
24    public static void Main()
25    {
26        Application.EnableVisualStyles();
27        Application.Run(new BitmapPixelForm());
28    }
29}

This approach is better when you are building up many pixels over time, such as in a drawing surface, image editor, or algorithm visualization.

Why CreateGraphics() Is Usually the Wrong Tool

A lot of examples on the internet use CreateGraphics() and draw directly outside the Paint event. That can work briefly, but the result is temporary. The next repaint wipes it out because the form does not know that your drawing should be restored.

So avoid code like this for persistent graphics:

csharp
var g = CreateGraphics();
g.FillRectangle(Brushes.Red, 50, 40, 1, 1);

This is acceptable only for very specialized immediate-mode scenarios, not for normal Windows Forms painting.

Performance Notes

For one pixel, any of these methods are fine. For thousands of pixels, Bitmap.SetPixel becomes slow because it performs per-pixel overhead on every call.

If you are drawing many pixels repeatedly, better choices include:

  • writing into bitmap memory in bulk
  • using LockBits
  • drawing higher-level primitives when possible

But for a single pixel or a small demo, SetPixel is perfectly reasonable and much easier to understand.

Common Pitfalls

The most common mistake is drawing outside the Paint event and then wondering why the pixel disappears after minimizing or resizing the window.

Another mistake is using DrawLine and being surprised by rendering behavior when smoothing, transforms, or pen settings are involved. A 1 x 1 rectangle or a bitmap pixel is clearer.

A third pitfall is ignoring DPI and scaling expectations. A logical pixel in your drawing code is still a single device unit in the graphics surface, but the visual size can be affected by how the UI is scaled on the display.

Summary

  • Draw a one-pixel rectangle in the Paint event for simple on-screen rendering.
  • Use a backing Bitmap when the pixel should persist as image data.
  • Avoid CreateGraphics() for normal persistent drawing in Windows Forms.
  • 'SetPixel is fine for small cases, but not ideal for large-scale pixel loops.'
  • In Windows Forms, the reliable solution is always repaint-aware drawing.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.