Porady: ręczne renderowanie buforowanej grafiki

Jeśli zarządzasz własną grafiką buforowaną, musisz mieć możliwość tworzenia i renderowania buforów graficznych. Możesz utworzyć wystąpienia BufferedGraphics klasy skojarzonej z powierzchniami rysunkowymi na ekranie, wywołując metodę Allocate . Ta metoda tworzy BufferedGraphics wystąpienie skojarzone z określoną powierzchnią renderowania, taką jak formularz lub kontrolka. Po utworzeniu wystąpienia można narysować grafikę BufferedGraphics do buforu reprezentowanego Graphics przez właściwość . Po wykonaniu wszystkich operacji graficznych można skopiować zawartość buforu na ekran, wywołując metodę Render .

Uwaga

Jeśli wykonasz własne renderowanie, zużycie pamięci wzrośnie, chociaż wzrost może być niewielki.

Aby ręcznie wyświetlić buforowaną grafikę

  1. Uzyskaj odwołanie do wystąpienia BufferedGraphicsContext klasy. Aby uzyskać więcej informacji, zobacz Instrukcje: Ręczne zarządzanie buforowaną grafiką.

  2. Utwórz wystąpienie BufferedGraphics klasy, wywołując metodę Allocate , jak pokazano w poniższym przykładzie kodu.

    // This example assumes the existence of a form called Form1.
    BufferedGraphicsContext currentContext;
    BufferedGraphics myBuffer;
    // Gets a reference to the current BufferedGraphicsContext
    currentContext = BufferedGraphicsManager.Current;
    // Creates a BufferedGraphics instance associated with Form1, and with
    // dimensions the same size as the drawing surface of Form1.
    myBuffer = currentContext.Allocate(this.CreateGraphics(),
       this.DisplayRectangle);
    
    ' This example assumes the existence of a form called Form1.
    Dim currentContext As BufferedGraphicsContext
    Dim myBuffer As BufferedGraphics
    ' Gets a reference to the current BufferedGraphicsContext.
    currentContext = BufferedGraphicsManager.Current
    ' Creates a BufferedGraphics instance associated with Form1, and with 
    ' dimensions the same size as the drawing surface of Form1.
    myBuffer = currentContext.Allocate(Me.CreateGraphics, _
       Me.DisplayRectangle)
    
    
  3. Rysuj grafikę do buforu graficznego Graphics , ustawiając właściwość . Przykład:

    // Draws an ellipse to the graphics buffer.
    myBuffer.Graphics.DrawEllipse(Pens.Blue, this.DisplayRectangle);
    
    ' Draws an ellipse to the graphics buffer.
    myBuffer.Graphics.DrawEllipse(Pens.Blue, Me.DisplayRectangle)
    
  4. Po zakończeniu wszystkich operacji rysowania do buforu graficznego wywołaj Render metodę renderowania buforu, do powierzchni rysunku skojarzonej z tym buforem lub do określonej powierzchni rysunku, jak pokazano w poniższym przykładzie kodu.

    // This example assumes the existence of a BufferedGraphics instance
    // called myBuffer.
    // Renders the contents of the buffer to the drawing surface associated
    // with the buffer.
    myBuffer.Render();
    // Renders the contents of the buffer to the specified drawing surface.
    myBuffer.Render(this.CreateGraphics());
    
    ' Renders the contents of the buffer to the drawing surface associated 
    ' with the buffer.
    myBuffer.Render()
    ' Renders the contents of the buffer to the specified drawing surface.
    myBuffer.Render(Me.CreateGraphics)
    
  5. Po zakończeniu renderowania grafiki wywołaj metodę Dispose w wystąpieniu BufferedGraphics , aby zwolnić zasoby systemowe.

    myBuffer.Dispose();
    
    myBuffer.Dispose()
    

Zobacz też