INFO: Resolution-Independence in GDI+ (311460)



The information in this article applies to:

  • Microsoft GDI+ 1.0
  • the operating system: Microsoft Windows XP 64-Bit Edition
  • Microsoft Windows XP Professional

This article was previously published under Q311460

SUMMARY

GDI+, and by extension the System.Drawing namespace in .NET Framework, is designed to be resolution-independent. Therefore, it is difficult to access individual output pixels because pixels are, by definition, resolution-dependent.

MORE INFORMATION

Even though it provides an abstraction from the hardware, the GDI+ predecessor, graphics device interface (GDI), is still fairly resolution-dependent. It is possible and even common when you use GDI to gain access to individual output pixels. GDI+, however, is not designed for a graphics programming paradigm in which direct access to individual output pixels is required.

Some of the features of GDI+ demonstrate this shift in paradigms. Consider interpolation mode, smoothing mode, page units, and world space transformations.

Also, note that the Graphics class has no SetPixel() method. It is possible to write code that sets the color at a logical coordinate, but GDI+ was not designed for code that accesses a single physical coordinate. For example, the following code attempts to affect one logical coordinate, but it may affect multiple output pixels if, for example, a scaling transform is in effect:
// This function, written in C#, uses Graphics.FillRectangle() to
// set the color of an individual logical output point. It uses
// aliased smoothing mode and a solid brush to obtain optimal performance
// and alter only the interesting point in world space.
private void SetPoint(System.Drawing.Graphics g, int nX, int nY, System.Drawing.Color c)
{
	SolidBrush brush = new SolidBrush( c );
	SmoothingMode smold = g.SmoothingMode;
	g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
	g.FillRectangle( brush, nX, nY, 1, 1 );
	g.SmoothingMode = smold;
}
				
Another method to obtain functionality similar to that of the SetPixel() method for a Graphics object is to create a 32-bits-per-pixel (bpp) bitmap of the image that you want, and then use the Graphics::DrawImage() method to display it on the output device by using an identity world-to-device transform. Bitmaps are inherently resolution-dependent and include a SetPixel() function and direct access to the image bits. The alpha channel in a 32-bpp bitmap allows you the latitude to transfer only the interesting part of the bitmap to the output device.

In summary, GDI+ is designed to be resolution-independent and to favor aesthetics over pixel-perfect output. For example, lines, text, and other drawings can be anti-aliased to improve their appearance. This new design does not afford easy access to individual output pixels, because the drawing code is designed to work in world space instead.

Modification Type:MinorLast Reviewed:4/3/2006
Keywords:kbDSWGDI2003Swept kbinfo KB311460