views:

426

answers:

3

How is Graphics.Save different from Graphics.BeginContainer?

+1  A: 

I am not giving you a solution but here are some links for your reference -

Graphics container document http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-drawing/2252/BeginContainer-and-Save

Graphics Containers

SUMMARY:The BeginContainer method of the Graphics class creates a container. Each BeginContainer method is paired with an EndContainer method. You can also create nested containers. The following code snippet creates two containers. A call to the Save method saves a GraphicsState object as an information block on the stack and returns it.

adatapost
Sure, but why do both exist? When would I want to choose one or the other? What specific elements of the state of the graphics object are saved/restored by each? I don't get it, and a link to a page where someone else says that they don't get it doesn't really help sort it out.
Doug McClean
A: 

Graphics.Save Method Saves the current state of this Graphics and identifies the saved state with a GraphicsState.

Graphics.BeginContainer Method Saves a graphics container with the current state of this Graphics object and opens and uses a new graphics container.

Remarks

Calls to the BeginContainer method place information blocks on the same stack as calls to the Save method. Just as a Restore call is paired with a Save call, a EndContainer method call is paired with a BeginContainer method call.

When you call the Restore method, all information blocks placed on the stack (by the Save method or by the BeginContainer method) after the corresponding call to the Save method are removed from the stack. Likewise, When you call the EndContainer method, all information blocks placed on the stack (by the Save method or by the BeginContainer method) after the corresponding call to the BeginContainer method are removed from the stack.

See details on http://msdn.microsoft.com/en-us/library/system.drawing.graphics.save.aspx

serhio
Sounds like similarities. I'm looking for differences. :)
Vulcan Eager
+1  A: 

take a look here:

The documentation does not differentiate between calls to BeginContainer/EndContainer and calls to Graphics.Save and GraphicsRestore. In addition, there are a few errors in the documentation. [e.g., GraphicsState is incorrectly asserted to be used by BeginContainer]

In my use, BeginContainer/EndContainer appears to save and restore the current transform. It does not actually save the clipping region as the documentation asserts, and it may not save any of the other properties in the graphics objects.

With Save/Restore, I was actually able to save/restore the clipping region, current transform, and other settings. It appears to be, if not complete, more "complete" than the container functions. Therefore, I suspect a performance/completeness tradeoff with the two different methods.

I also doubt whether the documentation is correct in stating that GraphicsState objects (used by Save) are stored in the stack as are GraphicsContainer objects (used by BeginContainer). I suspect that GraphicsState may not even be placed on a stack, but I have not tested this hypothesis.

serhio