Learn how to apply antialiasing on shapes using GDI+. Uses SmoothingMode and PixelOffsetMode enumerations.
Antialiasing = smoothing line edges in computer images: smoothing the jagged edges of diagonal lines in computer-generated images by varying the color or shades of gray at the edges
Microsoft® Encarta® Reference Library 2003. © 1993-2002 Microsoft Corporation. All rights reserved.
We all know what antialiasing is, probably most of us from games… when you try activating antialiasing in your favorite game the image is smoother but the graphics card lags so you go to the local store and $300 later you can play the game at a decent number of frames per second.
In some other tutorial at Geekpedia we used the OnPaint event to draw lines and other shapes on a form. As you probably saw, they weren’t very smooth because they didn’t had any antialiasing applied.
Using GDI+ it’s a piece of cake to apply antialiasing effect on this shapes. Create a new ‘Windows Application’ project in Microsoft Visual C# .NET and give the new form a white background color (using the BackColor property).
Also in the multitude of ‘using…’ statements add the following:
using System.Drawing.Drawing2D;
We will use this namespace for using the SmoothingMode and PixelOffsetMode properties.
And here is the code:
protected override void OnPaint(PaintEventArgs paintEvnt)
{
Graphics gfx = paintEvnt.Graphics;
Pen myPen = new Pen(Color.Black);
gfx.SmoothingMode = SmoothingMode.None;
gfx.DrawEllipse(myPen, 20, 20, 100, 100);
gfx.SmoothingMode = SmoothingMode.AntiAlias;
gfx.DrawEllipse(myPen, 150, 20, 100, 100);
gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
gfx.DrawEllipse(myPen, 20, 150, 100, 100);
}
SmoothingMode
The SmoothingMode property creates the antialiasing that makes the graphic object render smoothly.
You can choose from the following enumerations:
Default: Specifies the default mode.
AntiAlias: Specifies antialiased rendering.
HighQuality: Specifies high quality, low speed rendering.
HighSpeed: Specifies high speed, low quality rendering.
Invalid: Specifies an invalid mode.
None: Specifies no antialiasing.
HighQuality is actually the same as AntiAlias and HighSpeed is the same as None.
In the example code we used SmoothingMode.None and SmoothingMode.AntiAlias to demonstrate.
PixelOffsetMode
PixelOffsetMode, also known as an antialiasing enhancement tries to make the shapes a bit more smooth.
As its name implies, PixelOffsetMode offsets pixels to improve the quality.
Perhaps the easiest way to understand what PixelOffsetMode does is to analyze the screenshot of the code we have created in this tutorial.
In the following screenshot, the first circle (top, left) is created without antialias (SmoothingMode.None), the second (top, right) is created using antialiasing (SmoothingMode.AntiAlias) and the third (lower, left) is created with PixelOffsetMode set to HighQuality (PixelOffsetMode.HighQuality).
You can easily see the difference between the circle created only with SmoothingMode.AntiAlias and the circle created with PixelOffsetMode.HighQuality. The coordinates are decreased by the size of half a pixel. Overall the image created with SmoothingMode.AntiAlias and PixelOffsetMode.HighQuality is a bit better than the one created only with SmoothingMode.AntiAlias, of course with the cost of performance.
PixelOffsetMode has the following possible enumerations:
Default: Specifies the default mode.
Half: Specifies that pixels are offset by -.5 units, both horizontally and vertically, for high speed antialiasing.
HighQuality: Specifies high quality, low speed rendering.
HighSpeed: Specifies high speed, low quality rendering.
Invalid: Specifies an invalid mode.
None: Specifies no pixel offset.