Udostępnij za pośrednictwem


Instrukcje: ręczne renderowanie buforowanej grafiki

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

Nota

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 klasy BufferedGraphicsContext. Aby uzyskać więcej informacji, zobacz Instrukcje: Ręczne zarządzanie buforowaną grafiką.

  2. Utwórz wystąpienie klasy BufferedGraphics, 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, ustawiając właściwość Graphics. Na 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 grafiki wywołaj metodę Render w celu renderowania buforu na 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ż