How is Graphics.Save
different from Graphics.BeginContainer
?
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.
Maybe I can give a explanation from some examples of MSDN. The version of my MSDN is Visual Studio 2008 SP1. And the examples can be found when you enter the keyword "Nested Graphics Containers" in the edit of MSDN.
And its explanation is below:
As the two preceding examples show, transformations and clipping regions are cumulative in nested containers. If you set the world transformations of the container and the Graphics object, both transformations will apply to items drawn from inside the container. The transformation of the container will be applied first, and the transformation of the Graphics object will be applied second. If you set the clipping regions of the container and the Graphics object, items drawn from inside the container will be clipped by the intersection of the two clipping regions.
From the content copied above, the keywords are "cumulative" and "intersection". Therefore, I think it can be a way to understand the BeginContainer function.
The difference between BeginContainer()
and Save()
is only that BeginContainer()
resets all rendering properties to their default state.
So in brief BeginContainer()
does following:
- executes
Save()
internally to save the current state of graphics - reset all rendering properties (TextRenderingHint, InterpolationMode, SmoothingMode and so on, we don't know exact list of properties) to default their state.
You can find hint about it in the remarks section of documentation:
The graphics state established by the BeginContainer method includes the rendering qualities of the default graphics state; any rendering-quality state changes existing when the method is called are reset to the default values.
P.S.: This is not complete answer because we still don't know the full list of properties that are reset to default state, unfortunately documentation say nothing about it.
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
© 2022 - 2025 — McMap. All rights reserved.