Como Renderizar Elementos Gráficos em Buffer Manualmente

Se você gerenciar seus próprios elementos gráficos em buffer, precisará ser capaz de criar e renderizar buffers gráficos. Você pode criar instâncias da BufferedGraphics classe associada a superfícies de desenho na tela chamando o Allocate método. Esse método cria uma instância associada a uma BufferedGraphics superfície de renderização específica, como um formulário ou controle. Depois de criar uma BufferedGraphics instância, você pode desenhar elementos gráficos para o buffer que ela representa por meio da Graphics propriedade. Depois de executar todas as operações gráficas, você pode copiar o conteúdo do buffer para a tela chamando o Render método.

Observação

Se você executar sua própria renderização, o consumo de memória aumentará, embora o aumento possa ser pequeno.

Exibir elementos gráficos em buffer manualmente

  1. Obtenha uma referência a uma instância da BufferedGraphicsContext classe. Para obter mais informações, consulte Como gerenciar elementos gráficos em buffer manualmente.

  2. Crie uma instância da BufferedGraphics classe chamando o Allocate método, conforme mostrado no exemplo de código a seguir.

    // 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. Desenhe elementos gráficos para o buffer de gráficos definindo a Graphics propriedade. Por exemplo:

    // 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. Quando você tiver concluído todas as operações de desenho para o buffer de elementos gráficos, chame o método para renderizar o Render buffer, para a superfície de desenho associada a esse buffer ou para uma superfície de desenho especificada, conforme mostrado no exemplo de código a seguir.

    // 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. Depois de concluir a renderização de gráficos, chame o método na instância para liberar recursos do DisposeBufferedGraphics sistema.

    myBuffer.Dispose();
    
    myBuffer.Dispose()
    

Confira também