首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何获取表单的屏幕截图

如何获取表单的屏幕截图
EN

Stack Overflow用户
提问于 2011-02-26 09:23:54
回答 8查看 31.7K关注 0票数 28

有什么方法可以输出活动表单的屏幕截图吗?

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2011-02-26 09:41:26

由.NET支持的更简单的答案:

Control.DrawToBitmap

票数 28
EN

Stack Overflow用户

发布于 2011-02-26 09:46:27

使用Control.DrawToBitmap()方法。例如:

代码语言:javascript
复制
    private void timer1_Tick(object sender, EventArgs e) {
        var frm = Form.ActiveForm;
        using (var bmp = new Bitmap(frm.Width, frm.Height)) {
            frm.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
            bmp.Save(@"c:\temp\screenshot.png");
        }
    }
票数 28
EN

Stack Overflow用户

发布于 2011-02-26 09:30:42

试试这个:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace ScreenshotCapturer
{
    public partial class Form1 : Form
    {
        private static Bitmap bmpScreenshot;
        private static Graphics gfxScreenshot;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnCapture_Click(object sender, EventArgs e)
        {
            // If the user has choosed a path where to save the screenshot
            if (saveScreenshot.ShowDialog() == DialogResult.OK)
            {
                // Hide the form so that it does not appear in the screenshot
                this.Hide();
                // Set the bitmap object to the size of the screen
                bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
                // Create a graphics object from the bitmap
                gfxScreenshot = Graphics.FromImage(bmpScreenshot);
                // Take the screenshot from the upper left corner to the right bottom corner
                gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
                // Save the screenshot to the specified path that the user has chosen
                bmpScreenshot.Save(saveScreenshot.FileName, ImageFormat.Png);
                // Show the form again
                this.Show();
            }
        }
    }
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5124434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档