以编程方式截取网页屏幕截图
如何以编程方式将 URL 作为输入来截取网页的屏幕截图?
到目前为止,这就是我所拥有的:
// The size of the browser window when we want to take the screenshot (and the size of the resulting bitmap)
Bitmap bitmap = new Bitmap(1024, 768);
Rectangle bitmapRect = new Rectangle(0, 0, 1024, 768);
// This is a method of the WebBrowser control, and the most important part
webBrowser1.DrawToBitmap(bitmap, bitmapRect);
// Generate a thumbnail of the screenshot (optional)
System.Drawing.Image origImage = bitmap;
System.Drawing.Image origThumbnail = new Bitmap(120, 90, origImage.PixelFormat);
Graphics oGraphic = Graphics.FromImage(origThumbnail);
oGraphic.CompositingQuality = CompositingQuality.HighQuality;
oGraphic.SmoothingMode = SmoothingMode.HighQuality;
oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle oRectangle = new Rectangle(0, 0, 120, 90);
oGraphic.DrawImage(origImage, oRectangle);
// Save the file in PNG format
origThumbnail.Save(@"d:\Screenshot.png", ImageFormat.Png);
origImage.Dispose();
但这不起作用。它只给了我一张白色的空白图片。我在这里缺少什么?
有没有其他方法可以以编程方式获取网页的屏幕截图?
How do take a sceenshot of a webpage programmatically given the URL as input?
And here is what I have till now:
// The size of the browser window when we want to take the screenshot (and the size of the resulting bitmap)
Bitmap bitmap = new Bitmap(1024, 768);
Rectangle bitmapRect = new Rectangle(0, 0, 1024, 768);
// This is a method of the WebBrowser control, and the most important part
webBrowser1.DrawToBitmap(bitmap, bitmapRect);
// Generate a thumbnail of the screenshot (optional)
System.Drawing.Image origImage = bitmap;
System.Drawing.Image origThumbnail = new Bitmap(120, 90, origImage.PixelFormat);
Graphics oGraphic = Graphics.FromImage(origThumbnail);
oGraphic.CompositingQuality = CompositingQuality.HighQuality;
oGraphic.SmoothingMode = SmoothingMode.HighQuality;
oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle oRectangle = new Rectangle(0, 0, 120, 90);
oGraphic.DrawImage(origImage, oRectangle);
// Save the file in PNG format
origThumbnail.Save(@"d:\Screenshot.png", ImageFormat.Png);
origImage.Dispose();
But this is not working. It is only giving me a white blank picture. What am I missing here?
Is there any other way I could get the screenshot of a web page programmatically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我搜索啊搜索啊搜索,找到了网页缩略图 (代码项目文章)。
I searched and searched and searched and found it Webpage thumbnailer (a The Code Project article).
将浏览器控件绘制到位图有些不可靠。我认为最好只对窗口进行屏幕截图。
Drawing the browser control to a bitmap is somewhat unreliable. I think it would be better to just screenscrape your window.
您可以尝试调用本机
PrintWindow
函数。You can try calling the native
PrintWindow
function.您还可以尝试从
gdi32.dll
调用BitBlt()
。试试这个代码:P/Invoke 将是:
You could also try P/Invoking
BitBlt()
fromgdi32.dll
. Try this code:The P/Invoke would be: