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.
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.
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.
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:
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
Paintevent for simple on-screen rendering. - Use a backing
Bitmapwhen the pixel should persist as image data. - Avoid
CreateGraphics()for normal persistent drawing in Windows Forms. - '
SetPixelis 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
- Draw horizontal divider in winforms
- Draw solid color triangle using XAML only
- Duplicate keys in .NET dictionaries?
- dynamic does not contain a definition for a property from a project reference
- Easier way to populate a list with integers in .NET
- Easiest way to compare arrays in C
- EF Core add-migration Build Failed
- EF LINQ include multiple and nested entities

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.