This documentation is for the old version. Go to the latest Graphics Mill docs

Comparison: GdiGraphics and System.Drawing.Graphics

Graphics Mill for .NET provides quite rich drawing features via GdiGraphics class and other classes from Aurigma.GraphicsMill.Drawing namespace. Unlike standard .NET drawing classes which are based on GDI+ drawing enging, GdiGraphics class is based on Windows GDI engine. Both of these engines have their pros and contras. Here we compare GDI and GDI+ and provide guidelines when to use GdiGraphics and when - System.Drawing.Graphics.

Speed

The primary disadvantage GDI+ is usually blamed for is its low performance. For some operations, it may be slower than GDI tens and hundreds times. In fact GDI+ provides new features (like antialiasing, non-integer coordinates, etc), however they are often unnecessary for performance-critical applications.

For example, if you draw several tens of lines, shapes, or curves in Paint event of the window with GDI+, you may find that it workings too slow (delayed response for mouse events, flicker, etc). In the same way drawing them through GDI (and in particular GdiGraphics class) is much faster, with no noticeable delays.

Such high performance of GDI is nt achieved by using simpler and faster algorithms. For many operations hardware acceleration is used, while GDI+ does everything in the software.

Quality

Quality is a strong side of GDI+. When it draws lines, curves, and shapes it can use the antialiasing technique. It makes the drawing look smooth and high-quality.

GDI+ antialiasing.

However there is one aspect where GDI is stronger - text rendering. GDI+ implements custom text renderer which provides worse quality compared to GDI. It makes the text smoother, even for small font sizes. When font size is 8 or less, the text is quite hard to read because it looks too blurry. Unlike it, GDI is adaptive to the font size - it uses smoothing on big size text and disables it for small sizes.

Text Drawing Features

GDI+ has less text drawing features compared to GDI. First of all, it supports only TrueType fonts. If you need to use Type1 or OpenType fonts, you should use GdiGraphics.

Another GDI+ problem is that you cannot specify the exact position when drawing text. GDI+ adds some white space before the string. Also there is no way to increase intercharacter space in the string. GdiGraphics class allows that.

For the same reason GdiGraphics can get more details about font metrics. While GDI+ can return only basic details like ascent and descent, GDI can obtain more advanced information, including ABC metrics. Also, you can get exact position of the character in the string. All these features enable you to calculate precise coordinates and bound rectangles of the text and individual characters.

Alpha Blending

GDI+ has its own trump card - it handles alpha channel. This way it allows you to draw outlines and fill shapes with semitransparent brushes. Unfortunately, GDI does not allow this, and that is why GdiGraphics class cannot produce non-opaque drawing. Moreover, GDI cannot even handle bitmaps with an alpha channel when it draws anything on it. As the high byte of 4-byte color must be zero in GDI, each pixel drawn with GDI becomes transparent. So, if you try to call any drawing methods for the bitmaps with an alpha channel when using GDI, the UnsupportedPixelFormatException exception will be thrown.

However these alpha channel limitations are not an issue when drawing bitmaps with GdiGraphics. In fact GDI is bypassed and alpha blending is made by Graphics Mill for .NET, so it does not have such problems. An example of preserving an alpha channel can be found in the Adding Vignette topic.

Pixel Formats

System.Drawing.Graphics can be created on GDI+ bitmaps. However, not every bitmap can be used. In particular, it will fail with indexed bitmaps (1-bit, 4-bit, and 8-bit palette-based bitmaps).

GdiGraphics class can be opened over an indexed bitmap. It enables you to draw directly on indexed bitmaps without costly conversions into 24-bit RGB and back.

Unfortunately GdiGraphics also has limitations for pixel formats. It cannot draw on CMYK images, neither on 16-bit per channel ones. However it still supports more pixel formats than System.Drawing.Graphics.

Conclusion

GDI+ has such advantages as antialiased output and support of alpha channel. It makes it useful to create high-quality imagery. However for some kind of tasks common GDI works better. It gives higher performance, so it is good for real-time rendering, etc. Possibility to work directly with indexed colors makes it useful to operate with such kind of images as 1-bit documents (faxes, etc), web graphics (in GIF format), etc. Also, GDI provides better text rendering support.