.NET 2.0的WinForm打印屏幕屏幕、NET、WinForm

2023-09-04 06:47:44 作者:愿時光不負你

我要打印对话框的图像,就好像[ALT] [打印SCRN]被使用。该框架是否允许这种以编程方式完成?

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?

推荐答案

的的 Graphics.CopyFromScreen(..)的方法应该做你所需要的。

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-使用-Csharp.html

编辑: code样品:(我创建它作为一个扩展方法)

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);
    }
}

可以使用:

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

    private void button1_Click(object sender, EventArgs e)
    {
        this.SaveAsImage("foo.bmp", ImageFormat.Bmp);
    }
}