是否可以在Winforms中获得控件的屏幕截图而无需实际在屏幕上显示它? webBrowser组件如何?
最佳答案
是的。可以做到的。
我整理了一个示例程序来做。原谅我的命名约定和代码组织,这一点很快就解决了。您可以对其进行修改以更好地满足您的需求,但这显示了基础知识。
我有一个带有三个控件的表单:
这是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/