C# 以编程方式截取网页截图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2316564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Taking screenshot of a webpage programmatically
提问by Manish
How do take a sceenshot of a webpage programmatically given the URL as input?
给定 URL 作为输入,如何以编程方式拍摄网页的截图?
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?
有没有其他方法可以以编程方式获取网页的屏幕截图?
采纳答案by Manish
I searched and searched and searched and found it Webpage thumbnailer(a The Code Projectarticle).
回答by leppie
You can try calling the native PrintWindow
function.
您可以尝试调用本机PrintWindow
函数。
回答by Gabe
Drawing the browser control to a bitmap is somewhat unreliable. I think it would be better to just screenscrape your window.
将浏览器控件绘制到位图有点不可靠。我认为最好只对您的窗口进行屏幕抓取。
using (Bitmap bitmap = new Bitmap(bitmapSize.Width, bitmapSize.Height, PixelFormat.Format24bppRgb))
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(
PointToScreen(webBrowser1.Location),
new Point(0, 0),
bitmapSize);
bitmap.Save(filename);
}
回答by Simon Linder
You could also try P/Invoking BitBlt()
from gdi32.dll
. Try this code:
您也可以尝试BitBlt()
从gdi32.dll
. 试试这个代码:
Graphics mygraphics = webBrowser1.CreateGraphics();
Size s = new Size(1024, 768);
Bitmap memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
// P/Invoke call here
BitBlt(dc2, 0, 0, webBrowser1.ClientRectangle.Width, webBrowser1.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
memoryImage.Save(filename);
The P/Invoke would be:
P/Invoke 将是:
[DllImport("gdi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);