Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Antialiasing / Smoothing

Learn how to apply antialiasing on shapes using GDI+. Uses SmoothingMode and PixelOffsetMode enumerations.

On Wednesday, May 26th 2004 at 11:11 AM
By Andrew Pociu (View Profile)
*****   (Rated 4.2 with 43 votes)
Contextual Ads
More C# Resources
Advertisement

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.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by red on Friday, December 28th 2007 at 10:43 AM

first post!

by chris on Thursday, July 3rd 2008 at 12:08 AM

Thanks for the tips! I'm going to use it to make some antialiased graphs.
Cheers

by jeka911 on Saturday, January 31st 2009 at 08:58 AM

Thanks:)

by ChelOis on Saturday, March 14th 2009 at 10:10 AM

Excellent, much more better explanation than MSDN.
urgreat !!!

by ronak on Wednesday, April 1st 2009 at 01:04 AM

Excellent

by timo on Thursday, February 25th 2010 at 07:35 AM

Thank you!

by sda on Thursday, March 25th 2010 at 09:34 PM

asdasdasd

by mudassar on Thursday, March 25th 2010 at 09:35 PM

fucking information!!!! please improve your standard..


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia

Other Resources