10.9 Using Graphics Containers

 <  Day Day Up  >  

You want to manipulate the drawing of a shape without affecting any other shapes that are drawn concurrently.


When you rotate or translate the origin of the rendering surface, you affect any subsequent drawing operations. To save the transformation matrix and restore it when finished, use a GraphicsContainer object. Assign the result of calling the BeginContainer method defined in the Graphics class to a GraphicsContainer variable. Call the EndContainer method, passing the GraphicsContainer object as the parameter when you are finished rendering:

 private void DrawStar( Graphics g ) {     GraphicsContainer container = g.BeginContainer();     Point[] starPoints = { new Point(-20,0), new Point(20,0),         new Point(0,40), new Point(-20,0)};     for( int i = 0; i < 8; i++ )     {         g.FillPolygon( new SolidBrush(Color.Red), starPoints );         g.DrawPolygon( new Pen(new SolidBrush(Color.Black)), starPoints );         g.RotateTransform( 45.0f );     }     g.EndContainer( container ); } 


In the previous recipe, you used rotation and translation of the transformation matrix defined within the Graphics class to simplify the drawing of arbitrary shapes. A side effect of doing so, however, is that subsequent rendering happens on the rotated and translated matrix. To change this behavior, you have to save the current transformation matrix, perform any necessary rendering, and restore the original matrix when you are finished.

You use a GraphicsContainer object to change rendering options temporarily without affecting any subsequent operations. It might seem logical to just save the transformation matrix into a Matrix variable and use that matrix to restore the transformation matrix to its original state when finished. So you might be wondering why you should want to use a GraphicsContainer instead of a Matrix object. The GraphicsContainer also saves different graphics states that are currently in use. The current transformation matrix is one of those states, but others include any clipping regions , text rendering options, and the current composition mode. In other words, a GraphicsContainer is useful for saving and restoring a transformation matrix, but you should also use it if you need to temporarily change any rendering options without affecting the current state of the Graphics object for future rendering tasks .

 <  Day Day Up  >  

Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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