我得到了 WebBrowser 控件中包含的网页的屏幕截图,正如所描述的那样here http://www.dreamincode.net/code/snippet3389.htm and here https://stackoverflow.com/questions/3682635/take-screenshots-of-webbrowser-control,但我有一个问题:如果 WebBrowser 控件不是全屏,屏幕截图会被裁剪为控件的大小。
如果WebBrowser控件不是全屏,是否可以保存整个页面的屏幕截图?
这是我当前用于保存屏幕截图的代码:
private void SaveBrowserScreenshot(WebBrowser browser, string path, string name)
{
const int width = 1024;
const int height = 768;
const string extension = ".png";
using (var bitmap = new Bitmap(width, height))
{
var rect = new Rectangle(0, 0, width, height);
browser.DrawToBitmap(bitmap, rect);
using (var image = bitmap)
{
using (var graphic = Graphics.FromImage(image))
{
graphic.CompositingQuality = CompositingQuality.HighQuality;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
image.Save(String.Concat(path, "\\", name, extension), ImageFormat.Png);
}
}
}
}
性能不是问题,因为这永远不会在生产中使用。
由于我的 WebBrowser 控件被隐藏,我只需调整其大小即可解决此问题。如果控件未隐藏,则在保存屏幕截图时它将重新调整大小。
private void SaveBrowserScreenshot(WebBrowser browser, string path, string name)
{
const string extension = ".png";
int width = Math.Max(1024, Screen.PrimaryScreen.Bounds.Width);
int height = Math.Max(768, Screen.PrimaryScreen.Bounds.Height);
int originalWidth = browser.Width;
int originalHeight = browser.Heignt;
browser.Width = width;
browser.Heigt = height;
using (var bitmap = new Bitmap(width, height))
{
var rect = new Rectangle(0, 0, width, height);
browser.DrawToBitmap(bitmap, rect);
using (var image = bitmap)
{
using (var graphic = Graphics.FromImage(image))
{
graphic.CompositingQuality = CompositingQuality.HighQuality;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
image.Save(String.Concat(path, "\\", name, extension), ImageFormat.Png);
}
}
}
browser.Width = originalWidth;
browser.Height = originalHeight;
}
作为替代方案,您可以创建另一个 WebBrowser 控件,将其隐藏并按照 willDaBeast 的建议重新调整其大小,但这会对性能产生一些影响。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)