Advanced Capabilities of GDI


Advanced Capabilities of GDI+

I have only just touched on the many capabilities of GDI+. There is much more that you can do with it — far more than can be achieved in a single chapter. However, to round off this chapter, I will introduce several areas of these advanced capabilities.

Clipping

There are three contexts where clipping is important.

First, when the OnPaint() method is called, in addition to the Graphics object, the event is passed a clipping rectangle. For simple drawing routines, you don't need to pay much attention to this clipping rectangle, but if you have a very elaborate drawing routine that takes a lot of time, you can reduce this drawing time by testing against this clipping rectangle before you draw. You know the bounding rectangle of whatever graphic or figure that you need to draw. If this bounding rectangle does not intersect with the clipping rectangle, then you can skip the drawing operation.

Figure 30-17 shows one window containing a bar chart that is partially obscured by another window.

image from book
Figure 30-17

After the calculator has been closed, and after the Windows operating system has drawn the border of the window, the bar chart window looks like the one in Figure 30-18.

image from book
Figure 30-18

At this point, the OnPaint() method for the bar chart window would be called, with the clipping rectangle set to the area exposed by the closed window, shown in black in Figure 30-18. The bar chart would now need to draw the portions of its window that were previously underneath the overlying window. It would not need to redraw the car or trains bars, and in fact, even if the OnPaint() method tried to draw into the window in an area other than the exposed area, it could not. Any drawing that it did would be ignored. The bar chart window knows the bounding rectangle for the cars bar, and can determine if this rectangle intersects with the exposed portion of the window. Having determined that it does not intersect, the drawing routine will not redraw the part of the display covering the cars bar.

Sometimes when you need to draw only part of a figure or graphic, it is more convenient to draw the entire figure and clip the drawing to just what you want to see. You might have an image and want to draw just a portion of it. Rather than create a new image that is just a portion of the original image, you can set the clipping rectangle and then draw the image such that just the portion that you want to see shows through the clipping area. When creating a marquee, this is the technique you would use. By successively changing where you draw the text, and at the same time setting a clipping region, you can create the effect of horizontally moving text.

Finally, there is a technique where you can create a view port into a larger graphic. The user can move this view port around, by dragging the mouse on the graphic or by manipulating scroll bars. The view port also can be moved programmatically based on other actions that the user takes. In this case, setting a clipping region and drawing so that only what you want to see shows through is a good technique.

See the Clip property of the Graphics class in the .NET documentation for more information.

System.Drawing.Drawing2D

The classes in this namespace provide advanced two-dimensional and vector graphics functionality. You could use these classes to build a sophisticated drawing and image-processing application.

Note

Vector graphics are a technique where the programmer doesn't address pixels at all. Rather, the programmer records multiple vectors, indicating such operations as "draw from one point to another," "draw a rectangle at a certain location," and so on. Then the developer can apply scaling, rotation, and other transformations. Having applied the transformations, all the operations are rendered at once to the window.

This namespace includes:

  • Advanced brushes. You have already seen the LinearGradientBrush, and I touched on the PathGradientBrush. There is also a HatchBrush, which draws using a hatch style, a foreground color, and a background color.

  • The Matrix class, which defines geometric transforms. Using this class, you can do transforms on the drawing operations. For instance, using the Matrix class, you can draw an oval that is at a slant.

  • The GraphicsPath class, which was already touched on. Using this class, you can define a complex path and draw the entire path at once.

System.Drawing.Imaging

The classes in this namespace provide advanced imaging support, such as support for metafiles. A metafile describes a sequence of graphics operations that can be recorded and later played back, and there are classes within the System.Drawing.Imaging namespace that allow you to extend GDI+ to support other image formats.




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net