I am using WebKit.NET to integrate a browser component in my C# application. The problem is I can only capture the visible part in the browser window with a screenshot. Is there a way to capture the screenshot of the whole page?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Seems that it is kind of possible by using NativeMethods.SendMessage, although this can screw up the message queue, could you use http://cutycapt.sourceforge.net/ or perhaps http://iecapt.sourceforge.net/ or http://labs.awesomium.com/capturing-web-pages-with-c-net/?
回答2:
I use WebBrowser instead; ScrollBarsEnabled = false
let me capture whole page.
Here is some code:
protected override void Render(HtmlTextWriter writer)
{
StringBuilder builder = new StringBuilder();
HtmlTextWriter htw = new HtmlTextWriter(new StringWriter(builder));
base.Render(htw);
string html = builder.ToString();
_Generate(html);
}
private void _Generate(string html)
{
var browser = new WebBrowser { ScrollBarsEnabled = false };
DisplayHtml(html, browser);
browser.DocumentCompleted += WebBrowser_DocumentCompleted;
while (browser.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
browser.Dispose();
}
private void DisplayHtml(string html, WebBrowser browser)
{
browser.Navigate("about:blank");
if (browser.Document != null)
{
browser.Document.Write(string.Empty);
}
browser.DocumentText = html;
}
回答3:
Try This:
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.Windows.Forms;
public byte[] picbytes;
private void capture_Click(object sender, EventArgs e)
{
makepicture(txtURL.Text.Trim());
pictureBox1.Visible = true;
pictureBox1.Image = ByteToImage(picbytes);
}
private void makepicture(string url)
{
Thread thread = new Thread(delegate()
{
using (WebBrowser browser = new WebBrowser())
{
browser.ScrollBarsEnabled = false;
browser.AllowNavigation = true;
browser.Navigate(url);
browser.Width = 1024;
browser.Height = 1575;
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
using (Bitmap bitmap = new Bitmap(browser.Width, browser.Height))
{
browser.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, browser.Width, browser.Height));
using (MemoryStream stream = new MemoryStream())
{
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
byte[] bytes = stream.ToArray();
picbytes = bytes;
}
}
}
public static Bitmap ByteToImage(byte[] blob)
{
MemoryStream mStream = new MemoryStream();
byte[] pData = blob;
mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
Bitmap bm = new Bitmap(mStream, false);
mStream.Dispose();
return bm;
}