【问题标题】:screenshot of a winforms control through C#通过 C# 的 winforms 控件的屏幕截图
【发布时间】:2011-01-12 21:34:37
【问题描述】:

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

【问题讨论】:

  • 我不得不问 - 这是一个有趣的问题,但你为什么需要这样做呢?我真的很好奇,不是在取笑这个问题......
  • 我需要能够在满足某些条件时发送图形控制仪表的屏幕截图..
  • 为几个对象显示这些控件只是为了拍摄快照可能不是一个好主意。如果我可以将它们放在用户控件上,在后台拍摄它的快照,那就太好了并将其作为图像发送..

标签: winforms webbrowser-control screenshot


【解决方案1】:

是的。这是可以做到的。

我编写了一个示例程序来执行此操作。原谅我的命名约定和代码组织,这很快就被掀起了。您可以对其进行修改以更好地满足您的需求,但这显示了基础知识。

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

  • button1:具有默认设置的按钮。
  • button2:按钮的 Visible 属性设置为 false,Text 设置为“button2 - not visible”
  • 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");
        }

    }
}

这导致了以下捕获:

【讨论】:

  • 我不清楚。 Graphics g = myControl.CreateGraphics(); 是如何使用的?
猜你喜欢
  • 2010-12-02
  • 2010-11-24
  • 2021-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多