Compartir a través de


Representación manual de 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 asociada a superficies de dibujo en su pantalla llamando al método Allocate. Este método crea una BufferedGraphics instancia asociada a una superficie de representación determinada, como un formulario o control. Después de crear una BufferedGraphics instancia, puede dibujar gráficos en el búfer que representa a través de la Graphics propiedad . Después de realizar todas las operaciones gráficas, puede copiar el contenido del búfer en la pantalla llamando al método Render.

Nota:

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

Para mostrar manualmente gráficos en memoria intermedia

  1. Obtenga una referencia a una instancia de la BufferedGraphicsContext clase . Para obtener más información, vea Cómo: Administrar manualmente gráficos en buffer.

  2. Cree una instancia de la BufferedGraphics clase llamando al Allocate método , 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 gráficos en el búfer de gráficos configurando 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 Render método para representar el búfer, ya sea en la superficie de dibujo asociada a ese búfer o a una superficie de dibujo especificada, 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 gráficos, llame al Dispose método en la BufferedGraphics instancia para liberar recursos del sistema.

    myBuffer.Dispose();
    
    myBuffer.Dispose()
    

Consulte también