通过 C# 的 winforms 控件的屏幕截图 [英] screenshot of a winforms control through C#

查看:41
本文介绍了通过 C# 的 winforms 控件的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 winforms 中获取控件的屏幕截图而不实际显示在屏幕上?webBrowser 组件怎么样?

Is it possible to get a screenshot of a control in winforms without actually displaying it on the screen? How about a webBrowser component?

推荐答案

是的.这是可以做到的.

Yes. This can be done.

我编写了一个示例程序来完成它.请原谅我的命名约定和代码组织,这是非常迅速的.您可以对其进行修改以更好地满足您的需求,但这仅显示了基础知识.

I whipped up a sample program to do it. Forgive my naming conventions and code organization, this was whipped up very quickly. You can modify it to better fit your needs, but this shows the basics.

我有一个包含三个控件的表单:

I have a single form with three controls:

  • button1:具有默认设置的按钮.
  • button2:可见属性设置为 false 且文本设置为button2 - 不可见"的按钮
  • webBrowser1:将可见性设置为 false 的 WebBrowser 控件,将大小设置为 250, 101(只是为了适合我的表单.当您查看我的答案底部的捕获时,大小是相关的.您会需要相应地调整大小.)

这是 Form1 中的代码:

here's the code in Form1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {

            PrintInvisibleControl(button2, @"C:\button.jpg");
            PrintInvisibleControl(webBrowser1, @"C:\webbrowser.jpg");


        }

        private void PrintInvisibleControl(Control myControl, string filename)
        {

            Graphics g = myControl.CreateGraphics();
            //new bitmap object to save the image        
            Bitmap bmp = new Bitmap(myControl.Width, myControl.Height);
            //Drawing control to the bitmap        
            myControl.DrawToBitmap(bmp, new Rectangle(0, 0, myControl.Width, myControl.Height));
            bmp.Save(filename, ImageFormat.Jpeg);
            bmp.Dispose();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://www.microsoft.com");
        }

    }
}

这导致了以下捕获:

这篇关于通过 C# 的 winforms 控件的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆