分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
/// <summary> /// Capture IE full document. /// </summary> /// <param name="ieParams"> IE parameters. </param> /// <param name="savePath"> Save path. </param> /// <returns> Returns false if failed. </returns> internal static bool CaptureFullIEDocument(IEParams ieParams, string savePath) { try { IHTMLElement body = null; if (IEHelper.IsDtdDocument(ieParams.WebObj.Document) != false) { // For doc3 standard. IHTMLDocument3 doc = (IHTMLDocument3)ieParams.WebObj.Document; body = doc.documentElement; } else { // For doc2 standard IHTMLDocument2 doc = (IHTMLDocument2)ieParams.WebObj.Document; body = doc.body.parentElement; if ((int)body.getAttribute("clientHeight") == 0) { body = doc.body; } } body.setAttribute("scroll", "yes", 0); // Get Browser Window(web) Height and Width. int webHeight = (int)body.getAttribute("scrollHeight"); int webWidth = (int)body.getAttribute("scrollWidth"); // Get client's screen height and width, in pixels. int screenHeight = (int)body.getAttribute("clientHeight"); int screenWidth = (int)body.getAttribute("clientWidth"); // Set bitmaps to hold screen fragment. Bitmap screenFragment = new Bitmap(screenWidth, screenHeight, PixelFormat.Format16bppRgb555); // Target bitmap to draw into. Bitmap targetBitmap = new Bitmap(webWidth, webHeight, PixelFormat.Format16bppRgb555); Graphics gTarget = Graphics.FromImage(targetBitmap); Graphics g = null; // How many "pages" in this web. int page = 0; // Get the screen height (bottom up screen drawing). while ((page * screenHeight) < webHeight) { // - 5 -- for cutting the edge in IE6 when capture the screen by scrolling. body.setAttribute("scrollTop", (screenHeight - Edge) * page, 0); ++page; } // Rollback the page count by one. --page; // How many times you need to scroll horizontally. int pageWidthCount = 0; while ((pageWidthCount * screenWidth) < webWidth) { body.setAttribute("scrollLeft", (screenWidth - Edge) * pageWidthCount, 0); int scrollLeft = (int)body.getAttribute("scrollLeft"); // Loop through every pages. for (int i = page; i >= 0; --i) { // Shoot visible window. g = Graphics.FromImage(screenFragment); // Get handleof the device context. IntPtr hdc = g.GetHdc(); body.setAttribute("scrollTop", (screenHeight - Edge) * i, 0); int scrollTop = (int)body.getAttribute("scrollTop"); // Copy a visual window into the specified device context(DC). // hwnd - Handle to the window that will be copied. // hdc - Handle to the device context. // 0 - specifies the drawing options. PrintWindow(ieParams.HCurPage, hdc, 0); g.ReleaseHdc(hdc); g.Flush(); Image fragment = Image.FromHbitmap(screenFragment.GetHbitmap()); try { gTarget.DrawImage(fragment, scrollLeft, scrollTop); } catch (ArgumentNullException ex) { MessageBox.Show("Error occurred in DrawImage: " + ex); } } ++pageWidthCount; } const double Resolution = 100 * 0.01; // Set the resulution to 1:1. int finalWidth = (int)(webWidth * Resolution); int finalHeight = (int)(webHeight * Resolution); Bitmap finalImage = new Bitmap(finalWidth, finalHeight, PixelFormat.Format16bppRgb555); Graphics finalGraphic = Graphics.FromImage(finalImage); // Draw the final image. try { finalGraphic.DrawImage(targetBitmap, 0, 0, finalWidth, finalHeight); } catch (ArgumentNullException ex) { MessageBox.Show("Error occurred in drawing final Image: " + ex); } ImageHelper imageHelper = new ImageHelper(savePath); finalImage.Save(savePath, imageHelper.ImageCodecInfo, imageHelper.EncoderParameters); // clean up body = null; g.Dispose(); gTarget.Dispose(); finalGraphic.Dispose(); screenFragment.Dispose(); targetBitmap.Dispose(); finalImage.Dispose(); } catch (Exception ex) { throw new Exception(string.Format("Capture operation failed in the CaptureFullIEDocument function. The error message is: \n{0}", ex.Message), ex); } // Return true when success. return true; }