views:

442

answers:

3

I would like to print an image of a dialog, as if [alt][Print Scrn] were used. Does the framework allow for this to be done programmatically?

+5  A: 

The Graphics.CopyFromScreen(..) method should do what you need.

Here's a good sample I found on the web:

http://www.geekpedia.com/tutorial181_Capturing-screenshots-using-Csharp.html

EDIT: Code sample: (I created it as an extension method)

public static class FormExtensions
{
    public static void SaveAsImage(this Form form, string fileName, ImageFormat format)
    {
        var image = new Bitmap(form.Width, form.Height);
        using (Graphics g = Graphics.FromImage(image))
        {
            g.CopyFromScreen(form.Location, new Point(0, 0), form.Size);
        }
        image.Save(fileName, format);
    }
}

can be used:

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

    private void button1_Click(object sender, EventArgs e)
    {
        this.SaveAsImage("foo.bmp", ImageFormat.Bmp);
    }
}
BFree
Excellent! I found a less succinct solution that also works on http://www.dotnetcurry.com/ShowArticle.aspx?ID=303I modified the PrintScreen method by replacing Screen.PrimaryScreen with this. Thanks.
Blanthor
A: 

What you could probably do is use the existing DLL that has that functionality for windows. It looks like you need to grab either some key commands or do it with a form button, and use the User32.dll. Since interop can sometimes be a big pain, I found a resource here that might help you do what you want:

http://www.cornetdesign.com/2005/04/screen-print-capture-in-c-using_08.html

Wade
A: 

If you really want just the dialog, use Control.DrawToBitmap to get a BMP image from it.

Mark Brackett