您可以使用Control.DrawToBitmap(),即使它在 VisualStudio 中的 Intellisense 中是隐藏的。WebBrowser仍然继承自基类Control,所以这个方法确实存在。但我所做的是创建一个带有 MenuItem 的 MenuStrip,我用它来测试它(这基本上只是一个标准的单击事件),然后创建了一个图形对象,并使用正确的坐标复制了屏幕的一部分。您真正需要调整的唯一内容是 WebBrowser 控件的名称以及实际保存图像的行。
private void copyToolStripMenuItem_Click(object sender, EventArgs e) {
int width, height;
width = webBrowser1.ClientRectangle.Width;
height = webBrowser1.ClientRectangle.Height;
using (Bitmap image = new Bitmap(width, height)) {
using (Graphics graphics = Graphics.FromImage(image)) {
Point p, upperLeftSource, upperLeftDestination;
p = new Point(0, 0);
upperLeftSource = webBrowser1.PointToScreen(p);
upperLeftDestination = new Point(0, 0);
Size blockRegionSize = webBrowser1.ClientRectangle.Size;
graphics.CopyFromScreen(upperLeftSource, upperLeftDestination, blockRegionSize);
}
image.Save("C:\\Test.bmp");
}
}
Run Code Online (Sandbox Code Playgroud)