using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
this.Shown += new EventHandler(Form1_Shown);
}
WebBrowser webBrowser1 = new WebBrowser();
int Count = 0;
const int ClientWidth = 1024;
const int ClientHeight = 512;
void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Dock = DockStyle.Fill;
webBrowser1.ScrollBarsEnabled = false;
this.ClientSize = new Size(ClientWidth, ClientHeight);
this.Controls.Add(webBrowser1);
webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
void Form1_Shown(object sender, EventArgs e)
{
webBrowser1.Navigate("http://a...content-available-to-author-only...h.net/test/read.cgi/software/1335443552/");
}
void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
Interlocked.Increment(ref Count);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
Interlocked.Decrement(ref Count);
if (Count == 0)
{
this.SizeSet();
}
}
void SizeSet()
{
FormBorderStyle fbs = this.FormBorderStyle;
this.FormBorderStyle = FormBorderStyle.None;
Rectangle rect = webBrowser1.Document.Window.Document.Body.ScrollRectangle;
Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
int p = rect.Height / ClientHeight;
int s = rect.Height % ClientHeight;
Bitmap wbmp1 = new Bitmap(rect.Width, ClientHeight, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bmp))
{
for (int i = 0; i < p; i++)
{
webBrowser1.Document.Window.ScrollTo(0, ClientHeight * i);
this.DrawToBitmap(wbmp1, this.ClientRectangle);
//g.DrawImage(wbmp1, 0, ClientHeight * i);
}
if (s > 0)
{
this.ClientSize = new Size(ClientWidth, s);
webBrowser1.Document.Window.ScrollTo(0, ClientHeight * p);
Bitmap wbmp2 = new Bitmap(rect.Width, s, PixelFormat.Format32bppArgb);
this.DrawToBitmap(wbmp2, this.ClientRectangle);
g.DrawImage(wbmp2, 0, ClientHeight * p);
wbmp2.Dispose();
}
}
wbmp1.Dispose();
this.FormBorderStyle = fbs;
bmp.Save(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\test.png",
ImageFormat.Png);
bmp.Dispose();
}
}
}