How to: Manually Render Buffered Graphics

If you are managing your own buffered graphics, you will need to be able to create and render graphics buffers. You can create instances of the BufferedGraphics class that is associated with drawing surfaces on your screen by calling the Allocate method. This method creates a BufferedGraphics instance that is associated with a particular rendering surface, such as a form or control. After you have created a BufferedGraphics instance, you can draw graphics to the buffer it represents through the Graphics property. After you have performed all graphics operations, you can copy the contents of the buffer to the screen by calling the Render method.

Note

If you perform your own rendering, memory consumption will increase, though the increase may only be slight.

To manually display buffered graphics

  1. Obtain a reference to an instance of the BufferedGraphicsContext class. For more information, see How to: Manually Manage Buffered Graphics.

  2. Create an instance of the BufferedGraphics class by calling the Allocate method, as shown in the following code example.

    // 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. Draw graphics to the graphics buffer by setting the Graphics property. For example:

    // 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. When you have completed all of your drawing operations to the graphics buffer, call the Render method to render the buffer, either to the drawing surface associated with that buffer, or to a specified drawing surface, as shown in the following code example.

    // 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. After you are finished rendering graphics, call the Dispose method on the BufferedGraphics instance to free system resources.

    myBuffer.Dispose();
    
    myBuffer.Dispose()
    

See also