Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 17. Graphics with Windows Forms and GDI+ > Draw to an Off-Screen Buffer

Draw to an Off-Screen Buffer

Scenario/Problem:You want to draw GDI+ primitives to a bitmap for later display, printing, saving, or copying.
Solution:This sample code renders to both the screen and a bitmap for placement on the clipboard:
protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    Render(e.Graphics);
}

//can be called with a Graphics object
private void Render(Graphics graphics)
{
    graphics.FillEllipse(Brushes.Red, 10, 10, 100, 50);
}

private void button1_Click(object sender, EventArgs e)
{
    using (Bitmap bitmap =
             new Bitmap(ClientSize.Width, ClientSize.Height))
    using (Graphics graphics = Graphics.FromImage(bitmap))
    {
        Render(graphics);
        Clipboard.SetImage(bitmap);
    }
}

See the DrawToBitmap sample in the projects for this chapter for the complete example.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint