Procedimiento para representar manualmente gráficos almacenados en búfer

Si administra sus propios gráficos almacenados en búfer, deberá poder crear y representar búferes de gráficos. Puede crear instancias de la clase BufferedGraphics que está asociada con las superficies de dibujo en pantalla mediante llamadas al método Allocate. Este método crea una instancia BufferedGraphics que está asociada a una superficie de representación determinada, como un formulario o un control. Después de crear una instancia de BufferedGraphics, puede dibujar gráficos en el búfer que se representan mediante la propiedad Graphics. Después de haber realizado todas las operaciones de gráficos, puede copiar el contenido del búfer en la pantalla llamando al método Render.

Nota:

Si realiza su propia representación, aumentará el consumo de memoria, aunque este aumento puede ser ligero.

Para mostrar manualmente los gráficos almacenados en búfer

  1. Obtenga una referencia a una instancia de la clase BufferedGraphicsContext. Para obtener más información, consulte Procedimiento para administrar manualmente gráficos almacenados en búfer.

  2. Cree una instancia de la clase BufferedGraphics llamando al método Allocate, tal y como se muestra en el ejemplo de código siguiente.

    // 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. Dibuje los gráficos en el búfer de gráficos estableciendo la propiedad Graphics. Por ejemplo:

    // 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. Cuando haya completado todas las operaciones de dibujo en el búfer de gráficos, llame al método Render para representar el búfer, bien en la superficie de dibujo asociada con dicho búfer, o en una superficie de dibujo que especifique, tal y como se muestra en el ejemplo de código siguiente.

    // 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. Cuando haya terminado de representar los gráficos, llame al método Dispose en la instancia BufferedGraphics para liberar recursos del sistema.

    myBuffer.Dispose();
    
    myBuffer.Dispose()
    

Consulte también