Introduction
[Obsolete] - I am updating this to say that the below code is obsolete now that modern browsers all have tons of page capture extensions. I am leaving the code for historical/hysterical sake, but don't waste time trying to implement it. - Doug
--------------------------------------------------------------------------------------------------------------------------
This article presents a C# routine for capturing an entire web page as an image. Many capture examples show how to grab a screen shot, but do not show how to gather information that is below the scrolling region of an application. The most common example of a scrolling problem or “run-over” program is a web page.
This application grabs the page, plus, as a bonus, it demonstrates how to let the client adjust the size of the image and the quality of the JPEG. It shows how to write the name of the webpage onto the image, draw Standard Resolution Guides, save a bitmap as a JPEG and open the directory where the captures are stored.
Background
In a recent application, I wanted to provide our Quality Assurance testers the ability to capture an entire web page. I wanted them to do this by clicking a button from within a BHO (Browser Helper Object) that is used for another testing task. I also wanted to reduce the size of the capture, because the images are e-mailed and can quickly fill up our mailbox quotas.
Using the code
The easiest way to use this code is to download the source, trim out the code functions that may not be wanted (quality of capture, size of image, URL writing, guides, or the open directory function). After the code is trimmed down and the program can compile without errors, copy the source and its dependencies into the desired project.
The first issue to face when copying the source code into a project is the need to refer SHDocVw.dll and MSHTML.dll. In Visual Studio, go to Project, Add Reference, and then select the COM tab. Now, go down to the Microsoft section and look for "Microsoft Internet Controls". Select it, and then find "Microsoft HTML Object Library" (see the above image).
After adding the references, add these necessary directives into the project. (A few other directives are needed, if the code is not loaded into a form.)
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
using System.Drawing.Imaging;
using SHDocVw;
using mshtml;
Import user32 functions
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr FindWindowEx(IntPtr parent ,
IntPtr next , string sClassName, IntPtr sWindowTitle);
[DllImport("user32.dll", ExactSpelling=true, CharSet=CharSet.Auto)]
public static extern IntPtr GetWindow(IntPtr hWnd, int uCmd);
[DllImport("user32.Dll")]
public static extern void GetClassName(int h, StringBuilder s, int nMaxCount);
[DllImport("user32.dll")]
private static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, uint nFlags);
public const int GW_CHILD = 5;
public const int GW_HWNDNEXT = 2;
Find an open browser and assign a browser document for it.
SHDocVw.WebBrowser m_browser = null;
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();
string filename;
foreach (SHDocVw.WebBrowser ie in shellWindows)
{
filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
if (filename.Equals("iexplore"))
{
m_browser = ie;
break;
}
}
if (m_browser == null)
{
MessageBox.Show("No Browser Open");
return;
}
mshtml.IHTMLDocument2 myDoc = (mshtml.IHTMLDocument2)m_browser.Document;
The width and height of the web page must be determined along with the resolution settings of the clients screen.
myDoc.body.setAttribute("scroll", "yes", 0);
int heightsize = (int)myDoc.body.getAttribute("scrollHeight", 0);
int widthsize = (int)myDoc.body.getAttribute("scrollWidth", 0);
int screenHeight = (int)myDoc.body.getAttribute("clientHeight", 0);
int screenWidth = (int)myDoc.body.getAttribute("clientWidth", 0);
To capture the whole web page, fragments of the page will have to be grabbed and stitched together to make the whole page. After the first fragment is captured, the browser is scrolled down for the next capture. As the fragments are captured, they are stitched into a target bitmap. The process is repeated until the whole page is captured. For pages that are wider than the clients screen, the page gets scrolled over horizontally, and then the above process is repeated.
Bitmap bm = new Bitmap(screenWidth, screenHeight,
System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
Bitmap bm2 = new Bitmap(widthsize + URLExtraLeft, heightsize +
URLExtraHeight - trimHeight,
System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
Graphics g2 = Graphics.FromImage(bm2);
Graphics g = null;
IntPtr hdc;
Image screenfrag = null;
int brwTop = 0;
int brwLeft = 0;
int myPage = 0;
IntPtr myIntptr = (IntPtr)m_browser.HWND;
int hwndInt = myIntptr.ToInt32();
IntPtr hwnd = myIntptr;
hwnd = GetWindow(hwnd, GW_CHILD);
StringBuilder sbc = new StringBuilder(256);
while (hwndInt != 0)
{
hwndInt = hwnd.ToInt32();
GetClassName(hwndInt, sbc, 256);
if(sbc.ToString().IndexOf("Shell DocObject View", 0) > -1)
{
hwnd = FindWindowEx(hwnd, IntPtr.Zero,
"Internet Explorer_Server", IntPtr.Zero);
break;
}
hwnd = GetWindow(hwnd, GW_HWNDNEXT);
}
while ((myPage * screenHeight) < heightsize)
{
myDoc.body.setAttribute("scrollTop", (screenHeight - 5) * myPage, 0);
++myPage;
}
--myPage;
int myPageWidth = 0;
while ((myPageWidth * screenWidth) < widthsize)
{
myDoc.body.setAttribute("scrollLeft", (screenWidth - 5) * myPageWidth, 0);
brwLeft = (int)myDoc.body.getAttribute("scrollLeft", 0);
for (int i = myPage; i >= 0; --i)
{
g = Graphics.FromImage(bm);
hdc = g.GetHdc();
myDoc.body.setAttribute("scrollTop", (screenHeight - 5) * i, 0);
brwTop = (int)myDoc.body.getAttribute("scrollTop", 0);
PrintWindow(hwnd, hdc, 0);
g.ReleaseHdc(hdc);
g.Flush();
screenfrag = Image.FromHbitmap(bm.GetHbitmap());
g2.DrawImage(screenfrag, brwLeft + URLExtraLeft, brwTop +
URLExtraHeight);
}
++myPageWidth;
}
Finally, save the above target to a time stamped JPEG file.
Points of Interest
I had a lot of fun and suffered a lot of frustration with this project. The captures are really nice. Try it out on one of the "Code Project" pages.
Not shown in this article, but available in the source is the saving of the file to JPEG. I tried GIF and bitmap, but settled on JPEG for size. The main goal was to be able to e-mail these files without taking up a lot of our mailbox quota.
In the actual application, I have an option to copy the file to the clipboard. I never was able to get the clipboard image into a "device dependent bitmap" state that didn't take up much size. I would copy the image, and then paste it into my Outlook e-mail, only to have the e-mail be about a MB big. When I would open the JPEG in Photoshop, then select it, copy it and paste it into Outlook, the Adobe device dependent bitmap was under 100 KB. The same happened with the simple Windows Paintbrush application.
Because of time constraints, I settled on just copying the JPEG file to Outlook. Any solutions on how to turn a large device independent bitmap into a bitmap with a small memory footprint would be welcomed.