[Post] C # capture the webpage as an image

Source: Internet
Author: User

Reference (English) http://www.codeproject.com/cs/media/IECapture.asp
Environment: Visual Studio. NET 2003 language: C #
System Requirements: Windows + iexplore

TheProgramThe goal is to capture the web pages opened by IE browser and save them as images. We can customize the Image Storage Quality (control the image size ). Of course, we also need to consider that when multiple browser windows are opened, we capture the images of all windows.
Let's talk about how this program is implemented.

Step 1: Reference Standard DLL

This program requires shdocvw. DLL and mshtml. DLL support, so we need to add two COM components in our project, select the com tab in the Add reference dialog box, and then find Microsoft Internet controls ". and Microsoft HTML Object Library.

Step 2: add necessary namespaces

Using system. text;
Using system. runtime. interopservices;
Using system. diagnostics;
Using system. IO;
Using system. Drawing. imaging;
Using shdocvw;
Using mshtml;

Step 3: Call the user32.dll Function

[Dllimport ("user32.dll", charset = charset. Auto)]
Public static extern intptr find1_wex (intptr parent/** // * hwnd */,
Intptr next/** // * hwnd */, 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;

Step 4: find an open browser process and assign it a browser document.

Shdocvw. webbrowser m_browser = NULL;
Shdocvw. shellwindows = new shdocvw. shellwindowsclass ();

// Find first availble browser window.
// Application can easily be modified to loop through and
// Capture all open windows.
String filename;
Foreach (shdocvw. webbrowser IE in shellwindows)
{
Filename = path. getfilenamewithoutextension (ie. fullname). tolower ();
If (filename. Equals ("istme "))
{
M_browser = ie;
Break;
}
}
If (m_browser = NULL)
{
MessageBox. Show ("No browser open ");
Return;
}

// Assign browser document
Mshtml. ihtmldocument2 mydoc = (mshtml. ihtmldocument2) m_browser.document;

Step 5: Get the height and width of the screen and webpage

// Set Scrolling on.
Mydoc. Body. setattribute ("scroll", "yes", 0 );

// Get browser window height
Int heightsize = (INT) mydoc. Body. getattribute ("scrollheight", 0 );
Int widthsize = (INT) mydoc. Body. getattribute ("scrollwidth", 0 );

// Get screen height
Int screenheight = (INT) mydoc. Body. getattribute ("clientheight", 0 );
Int screenwidth = (INT) mydoc. Body. getattribute ("clientwidth", 0 );

Step 6: CaptureAlgorithm

It should be noted that there are actually no components that allow us to directly capture the webpage completely. We still capture the page from the basic one page, then the entire image is combined.
The basic idea is as follows: first capture the first segment of the web page, then the scroll bar slide on the next page, continue to capture the next segment, and then combine the segment into a target MAP, just like stitching up. This process repeats until the last segment is captured and stitched to the target map.
Of course, we may also encounter a situation where the webpage is wider than the screen. This is the first clip of our program, and then the browser scroll bar moves horizontally to capture the rest, then, connect and perform the preceding steps.
// Get bitmap to hold screen fragment.
Bitmap Bm = new Bitmap (screenwidth, screenheight,
System. Drawing. imaging. pixelformat. format16bpprgb555 );

// Create a target bitmap to draw.
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;

// Get inner browser window.
Int hwndint = myintptr. toint32 ();
Intptr hwnd = myintptr;
Hwnd = getwindow (hwnd, gw_child );
Stringbuilder SBC = new stringbuilder (256 );

// Get Browser "document" Handle
While (hwndint! = 0)
{
Hwndint = hwnd. toint32 ();
Getclassname (hwndint, SBC, 256 );

If (SBC. tostring (). indexof ("shell docobject View", 0)>-1)
{
Hwnd = find1_wex (hwnd, intptr. Zero,
"Internet assumer_server", intptr. Zero );
Break;
}
Hwnd = getwindow (hwnd, gw_hwndnext );
}

// Get screen height (for bottom up Screen Drawing)
While (mypage * screenheight) {
Mydoc. Body. setattribute ("scrolltop", (screenheight-5) * mypage, 0 );
++ Mypage;
}

// Rollback the page count by one
-- 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)
{
// Shoot visible window
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.