Condividi tramite


Procedura: eseguire il rendering manuale di grafica memorizzata nel buffer

Se si sta gestendo direttamente grafica memorizzata nel buffer, sarà necessario essere in grado di creare ed eseguire il rendering dei buffer grafici. È possibile creare istanze della classe BufferedGraphics associata alle aree di disegno sullo schermo chiamando il metodo Allocate. Il metodo crea un'istanza di BufferedGraphics associata a una particolare area di rendering, ad esempio un form o controllo. Una volta creata un'istanza di BufferedGraphics, è possibile disegnare la grafica nel buffer da essa rappresentato tramite la proprietà Graphics. Una volta eseguite tutte le operazioni grafiche, è possibile copiare il contenuto del buffer sullo schermo chiamando il metodo Render.

Nota

Se si esegue direttamente il rendering, l'uso di memoria aumenterà, anche se solo leggermente.

Per visualizzare manualmente la grafica memorizzata nel buffer

  1. Ottenere un riferimento a un'istanza della classe BufferedGraphicsContext. Per altre informazioni, vedere Procedura: Gestire manualmente la grafica memorizzata nel buffer.

  2. Creare un'istanza della classe BufferedGraphics chiamando il metodo Allocatee, come illustrato nel codice di esempio seguente.

    // 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. Disegnare la grafica nel buffer relativo impostando la proprietà Graphics. Ad esempio:

    // 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. Una volta completate tutte le operazioni di disegno nel buffer grafico, chiamare il metodo Render per eseguire il rendering del buffer, nell'area di disegno associata al buffer, oppure in un'area specificata, come illustrato nell'esempio di codice seguente.

    // 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. Una volta terminato il rendering della grafica, chiamare il metodo Dispose sull'istanza di BufferedGraphics per liberare risorse di sistema.

    myBuffer.Dispose();
    
    myBuffer.Dispose()
    

Vedi anche