Rendering form to bitmap

2019-01-20 13:13发布

问题:

I would like to render Form to bitmap... Is it possible in .net 2.0?

回答1:

Sure, you can call Control.DrawToBitmap() to render a control to a bitmap. For more general drawing, you can create a Bitmap and then use Graphics.FromImage() to create a Graphics instance. You can then draw to this graphics instance as normal.

Here's a simple form that can draw itself (just add a button and double click it to add the Click event handler:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Bitmap b = new Bitmap(Width, Height);
        DrawToBitmap(b, new Rectangle(0, 0, Width, Height));
        b.Save("Test.bmp");
    }
}