Język

Graphics.Save Metoda

Definicja

Zapisuje bieżący stan tej Graphics funkcji i identyfikuje zapisany stan za pomocą .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

Zwraca

Ta metoda zwraca wartość GraphicsState reprezentującą zapisany stan tego Graphicsobiektu .

Przykłady

Poniższy przykład kodu jest przeznaczony do użycia z Windows Forms i wymaga PaintEventArgse, który jest parametrem programu obsługi zdarzeń Paint. Kod wykonuje następujące akcje:

  • Przekształca świat Windows Form przez wektor (100, 0).

  • Zapisuje stan grafiki formularza.

  • Resetuje świat transformacji formularza do tożsamości (macierz tożsamości 2x2 i translacja wektorów zerowych) i wypełnia prostokąt stałym czerwonym pędzlem.

  • Przywraca przetłumaczony stan grafiki i wypełnia prostokąt niebieskim pędzlem.

Wynik jest nieprzetłumaczonym czerwonym prostokątem po lewej stronie i przetłumaczonym niebieskim prostokątem po prawej stronie formularza.

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

Uwagi

Podczas wywoływania Save metody Graphics, blok informacji, który przechowuje stan obiektu Graphics , jest umieszczany na stosie. Metoda Save zwraca wartość GraphicsState , która identyfikuje ten blok informacji. Po przekazaniu identyfikatora GraphicsState do Restore metody blok informacji zostanie usunięty ze stosu i zostanie użyty do przywrócenia Graphics stanu, w którym znajdował się w momencie Save wywołania metody. Należy pamiętać, że zwrócone GraphicsState przez podane wywołanie metody można przekazać Save tylko raz do Restore metody.

Save Wywołania metody można zagnieżdżać. Oznacza to, że można wywołać metodę kilka razy przed wywołaniem SaveRestore metody . Za każdym razem, gdy wywołujesz metodę Save , blok informacji jest umieszczany na stosie i otrzymujesz element GraphicsState dla bloku informacji. Po przekazaniu jednego z tych obiektów do Restore metody obiekt jest zwracany do stanu, w którym znajdował się w momencie Graphics wywołania metody, Save które zwróciło ten konkretny GraphicsStateelement . Blok informacji umieszczony na stosie przez Save to wywołanie metody jest usuwany ze stosu, a wszystkie bloki informacji umieszczone na tym stosie po wywołaniu Save metody również zostaną usunięte.

BeginContainer Wywołania metody umieszczają bloki informacji na tym samym stosie co wywołania Save metody . Podobnie jak Restore wywołanie jest sparowane z Save wywołaniem, EndContainer wywołanie metody jest sparowane z wywołaniem BeginContainer metody.

Po wywołaniu Restore metody wszystkie bloki informacji umieszczone na stosie (za pomocą Save metody lub BeginContainer metody) po usunięciu odpowiedniego wywołania Save metody z stosu. Podobnie po wywołaniu EndContainer metody wszystkie bloki informacji umieszczone na stosie (za pomocą Save metody lub BeginContainer metody) po usunięciu odpowiedniego wywołania BeginContainer metody z stosu.

Dotyczy