【问题标题】:.NET 2.0 WinForm print screen.NET 2.0 WinForm 打印屏幕
【发布时间】:2009-06-17 16:44:24
【问题描述】:

我想打印一个对话框的图像,好像使用了 [alt][Print Scrn]。该框架是否允许以编程方式完成此操作?

【问题讨论】:

    标签: c# .net winforms printing


    【解决方案1】:

    Graphics.CopyFromScreen(..) 方法应该可以满足您的需求。

    这是我在网上找到的一个很好的示例:

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

    编辑:代码示例:(我将其创建为扩展方法)

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

    【讨论】:

    • 太棒了!我找到了一个不太简洁的解决方案,它也适用于dotnetcurry.com/ShowArticle.aspx?ID=303 我修改了 PrintScreen 方法,用它替换了 Screen.PrimaryScreen。谢谢。
    【解决方案2】:

    您可能会做的是使用具有该功能的现有 DLL 用于 Windows。看起来您需要获取一些关键命令或使用表单按钮执行此操作,并使用 User32.dll。由于互操作有时会很痛苦,因此我在这里找到了一个资源,可以帮助您做您想做的事:

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

    【讨论】:

      【解决方案3】:

      如果您真的只想要对话框,请使用Control.DrawToBitmap 从中获取 BMP 图像。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多