Click here to Skip to main content
15,868,016 members
Articles / Multimedia / GDI
Article

Antialiasing Using Windows GDI

Rate me:
Please Sign up or sign in to vote.
4.80/5 (23 votes)
1 Dec 2007CPOL2 min read 107K   7.2K   71   10
An article on a simple but efficient method to do antialiasing using plain Windows GDI
Screenshot - Sample_1.jpg
Screenshot - Sample_2.jpg

Introduction

This article is about a simple but effective way to perform real-time antialiasing (yes, real-time) using just plain Windows GDI. There is no default method under GDI to do this directly. All GDI methods like LineTo(), Ellipse() etc. will perform their drawing but will take no antialiasing into consideration when there comes rendering pixels to memory or screen bitmaps. How to do this then? Well, here is the possible answer to this question.

Background

There are some (but not many) articles on The Code Project which speak about the antialiasing problem when performing drawing under Windows GDI. In fact, the antialiasing should produce a high quality rendering with no jagged-edges visible. It costs more CPU cycles to finish the drawing, but at the end it looks just great. For a detailed explanation of the aliasing problem and the possible solution, please read CT Graphics - Anti Alias C++ Drawing or Antialiasing - Wu Algorithm. One will find enough information to build her or his own antialiasing rendering method, no doubt.

Using the Code

There is no special class or method provided in this article, only the general algorithm which is very easy to implement and the demo project in the download section. Here is the pseudo-code:

//
// 1. Create original bitmap for drawing
// 2. Create temporary bitmap (2x, 4x or 8x) larger than the original bitmap
// 3. Render your graphics to this large temporary bitmap 
//        (use any GDI method, pen or brush you like) 
//        but scale the graphics appropriately
// 4. Draw this temporary bitmap on the original bitmap scaled 
//        (i.e. using StretchDIBits() method or any other you like), 
//        but call SetStretchBltMode(HALFTONE)
//        before this last step for the original DC 
//        (which holds the original bitmap), and after scaling restore it back
//

After completing these four steps, you have done it. You'll get very nice graphics on the destination bitmap.

What About the Price

When it comes to the price of applying the described method, there are a few things that one must take into consideration:

  • Since the Windows GDI is not a speed recorder, when it comes to rendering, the original bitmap must have some reasonable dimensions, say 640*480 pixels to use 4x4 antialiasing by supersampling, or 320*240 to use 8x8 antialiasing by supersampling. These settings will work in a real-time loop with almost no optimizations, but if you prefer larger high-quality rendered pictures then forget about the real-time rendering using GDI. It's just too slow for this situation. However, if you don't have a real-time project to deal with, you can render an image of any size you like. Only a free RAM available on your system can stop you then.
  • Increasing the antialiasing grid from 2x2 to 4x4 increases the memory consumption 4 times, and that is 16 times more memory then you need to hold the original bitmap. This is the real price you pay for using this simple method.
  • For animations, keep low antialiasing grid size, no more then 2x2. This gives quite good rendering results with a good frame rate too.

Points of Interest

I have found a simple way to perform antialiasing using just Windows GDI with no third party libraries, no GDI+ etc. It was very interesting to compare the final results with the commercial libraries.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Elektromehanika d.o.o. Nis
Serbia Serbia
He has a master degree in Computer Science at Faculty of Electronics in Nis (Serbia), and works as a C++/C# application developer for Windows platforms since 2001. He likes traveling, reading and meeting new people and cultures.

Comments and Discussions

 
PraiseGood Job, a little change maybe Pin
Kyle.CY30-May-18 21:54
Kyle.CY30-May-18 21:54 
GeneralRe: Good Job, a little change maybe Pin
Southmountain10-Jan-21 10:48
Southmountain10-Jan-21 10:48 
Questionlinks are dead Pin
Member 568844323-Jun-17 3:12
Member 568844323-Jun-17 3:12 
AnswerRe: links are dead Pin
Rick York23-Jun-17 4:42
mveRick York23-Jun-17 4:42 
GeneralCongratulations ! Pin
Alexandre Crivelente23-Jan-13 13:58
Alexandre Crivelente23-Jan-13 13:58 
GeneralBrilliant idea Pin
darkevil82826-Jan-09 3:18
darkevil82826-Jan-09 3:18 
GeneralRe: Brilliant idea Pin
darkoman6-Jan-09 6:39
darkoman6-Jan-09 6:39 
GeneralNot perfect Pin
Graham Reeds4-Dec-07 4:06
Graham Reeds4-Dec-07 4:06 
The ends of the lines seem to move up and to the left. Also you lose energy of the colour itself on the line.

However that leads into a lot more complicated issue of actual antialiasing. For the simplicity it is quite good.

G.
AnswerRe: Not perfect Pin
GoogleMaster23-Jun-09 0:14
GoogleMaster23-Jun-09 0:14 
GeneralSimple idea Pin
NormDroid1-Dec-07 22:14
professionalNormDroid1-Dec-07 22:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.