Partager via


Graphics.Save Méthode

Définition

Enregistre l’état actuel de cette Graphics et identifie l’état enregistré avec un GraphicsState.

public:
 System::Drawing::Drawing2D::GraphicsState ^ Save();
public System.Drawing.Drawing2D.GraphicsState Save ();
member this.Save : unit -> System.Drawing.Drawing2D.GraphicsState
Public Function Save () As GraphicsState

Retours

Cette méthode retourne un GraphicsState qui représente l’état enregistré de cette Graphics.

Exemples

L’exemple de code suivant est conçu pour être utilisé avec Windows Forms et nécessite PaintEventArgse, qui est un paramètre du gestionnaire d’événements Paint. Le code effectue les actions suivantes :

  • Traduit la transformation mondiale du Windows Form par un vecteur (100, 0).

  • Enregistre l’état graphique du formulaire.

  • Réinitialise la transformation du monde du formulaire à une identité (matrice d’identité 2x2 plus une traduction de vecteur zéro) et remplit un rectangle avec un pinceau rouge plein.

  • Restaure l’état graphique traduit et remplit un rectangle avec un pinceau bleu unie.

Le résultat est un rectangle à remplissage rouge non traduit à gauche et un rectangle bleu traduit à droite du formulaire.

public:
   void SaveRestore3( PaintEventArgs^ e )
   {
      // Translate transformation matrix.
      e->Graphics->TranslateTransform( 100, 0 );

      // Save translated graphics state.
      GraphicsState^ transState = e->Graphics->Save();

      // Reset transformation matrix to identity and fill rectangle.
      e->Graphics->ResetTransform();
      e->Graphics->FillRectangle( gcnew SolidBrush( Color::Red ), 0, 0, 100, 100 );
      
      // Restore graphics state to translated state and fill second
      // rectangle.
      e->Graphics->Restore( transState );
      e->Graphics->FillRectangle( gcnew SolidBrush( Color::Blue ), 0, 0, 100, 100 );
   }
private void SaveRestore3(PaintEventArgs e)
{

    // Translate transformation matrix.
    e.Graphics.TranslateTransform(100, 0);

    // Save translated graphics state.
    GraphicsState transState = e.Graphics.Save();

    // Reset transformation matrix to identity and fill rectangle.
    e.Graphics.ResetTransform();
    e.Graphics.FillRectangle(new SolidBrush(Color.Red), 0, 0, 100, 100);

    // Restore graphics state to translated state and fill second

    // rectangle.
    e.Graphics.Restore(transState);
    e.Graphics.FillRectangle(new SolidBrush(Color.Blue), 0, 0, 100, 100);
}
Private Sub SaveRestore3(ByVal e As PaintEventArgs)

    ' Translate transformation matrix.
    e.Graphics.TranslateTransform(100, 0)

    ' Save translated graphics state.
    Dim transState As GraphicsState = e.Graphics.Save()

    ' Reset transformation matrix to identity and fill rectangle.
    e.Graphics.ResetTransform()
    e.Graphics.FillRectangle(New SolidBrush(Color.Red), 0, 0, 100, 100)

    ' Restore graphics state to translated state and fill second

    ' rectangle.
    e.Graphics.Restore(transState)
    e.Graphics.FillRectangle(New SolidBrush(Color.Blue), 0, 0, _
    100, 100)
End Sub

Remarques

Lorsque vous appelez la méthode Save d’un Graphics, un bloc d’informations qui contient l’état du Graphics est placé sur une pile. La méthode Save retourne un GraphicsState qui identifie ce bloc d’informations. Lorsque vous transmettez les GraphicsState d’identification à la méthode Restore, le bloc d’informations est supprimé de la pile et est utilisé pour restaurer l'Graphics à l’état dans lequel il était au moment de l’appel de méthode Save. Notez que le GraphicsState retourné par un appel donné à la méthode Save ne peut être transmis qu’une seule fois à la méthode Restore.

Les appels à la méthode Save peuvent être imbriqués ; autrement dit, vous pouvez appeler la méthode Save plusieurs fois avant d’appeler la méthode Restore. Chaque fois que vous appelez la méthode Save, un bloc d’informations est placé sur la pile et vous recevez un GraphicsState pour le bloc d’informations. Lorsque vous passez l’un de ces objets à la méthode Restore, le Graphics est retourné à l’état dans lequel il était au moment de l’appel de méthode Save qui a retourné ce GraphicsStateparticulier. Le bloc d’informations placé sur la pile par ce Save appel de méthode est supprimé de la pile, et tous les blocs d’informations placés sur cette pile après cet appel de méthode Save sont également supprimés.

Les appels à la méthode BeginContainer placent des blocs d’informations sur la même pile que les appels à la méthode Save. Tout comme un appel Restore est associé à un appel Save, un appel de méthode EndContainer est associé à un appel de méthode BeginContainer.

Lorsque vous appelez la méthode Restore, tous les blocs d’informations placés sur la pile (par la méthode Save ou par la méthode BeginContainer) après l’appel correspondant à la méthode Save sont supprimés de la pile. De même, lorsque vous appelez la méthode EndContainer, tous les blocs d’informations placés sur la pile (par la méthode Save ou par la méthode BeginContainer) après l’appel correspondant à la méthode BeginContainer sont supprimés de la pile.

S’applique à