winforms - C#的winforms控件的屏幕截图

标签 winforms webbrowser-control screenshot

是否可以在Winforms中获得控件的屏幕截图而无需实际在屏幕上显示它? webBrowser组件如何?

最佳答案

是的。可以做到的。

我整理了一个示例程序来做。原谅我的命名约定和代码组织,这一点很快就解决了。您可以对其进行修改以更好地满足您的需求,但这显示了基础知识。

我有一个带有三个控件的表单:

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

  • 这是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");
            }
    
        }
    }
    

    这导致了以下捕获:

    关于winforms - C#的winforms控件的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4674302/

    相关文章:

    ios - drawViewHierarchyInRect 与 renderInContext 的预期性能是什么

    winforms - 具有新的 Office High DPI 支持的 Outlook VSTO

    c# - Winforms:如何在透明窗体上获取鼠标事件以进行透明控制

    c# - 当控件聚焦时绘制边框

    Python:使用 Chrome 浏览器进行操作

    c# - 使用两点捕获屏幕截图

    c# - 将 "*"值放入对话框 C# 表单中

    c# - 网络浏览器 : sequencing activites when no DocumentCompleted is fired by a link on hosted webpage

    c# - .NET Web 浏览器控件和 Dispose()

    java - 如何使用 Java 显示计算机的桌面?