参考原文(英文) http://www.codeproject.com/cs/media/IECapture.asp
环境:Visual Studio.NET 2003 语言:C#
系统需求:Windows + iexplore                                                       附件:捕获网页为图像程序
捕获效果图:捕获网页为图像

         该程序的目标很明确,就是捕获IE浏览器打开的网页存为图像,附加的需求,我们能自定义图像的保存质量(控制图像的大小)。当然我们还要考虑当有多个浏览器窗口打开的时候,我们是捕获所有窗口的图像。
        废话不多说,我们开始说说这个程序如何实现。

第一步:
         该程序需要SHDocVw.dll 和 MSHTML.dll的支持,所以在我们的工程中需要添加两个com组件,在添加引用的对话框中选择com的标签页,然后找到Microsoft Internet Controls". 和Microsoft HTML Object Library,添加进来。

第二步:添加必要的名称空间     

捕获网页为图像using  System.Text;
捕获网页为图像
using  System.Runtime.InteropServices;
捕获网页为图像
using  System.Diagnostics;
捕获网页为图像
using  System.IO;
捕获网页为图像
using  System.Drawing.Imaging;
捕获网页为图像
using  SHDocVw;
捕获网页为图像
using  mshtml;

第三步:调用user32.dll的函数
捕获网页为图像[DllImport( " user32.dll " , CharSet = CharSet.Auto)]
public   static   extern  IntPtr FindWindowEx(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 ;

第四步:找到一个打开的浏览器进程,并分配一个Browser Document给它。

捕获网页为图像SHDocVw.WebBrowser m_browser  =   null ;
捕获网页为图像 SHDocVw.ShellWindows 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( " iexplore " ))
      
捕获网页为图像 {
捕获网页为图像          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;

第五步:获取屏幕和网页的高度和宽度
捕获网页为图像  // 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 );

第六步:捕获算法
         这里要说明的是,其实没有什么组件可以让我们直接把网页完全捕获过来,我们还是从基本的一页一页捕捉,然后组合整个图像的方法来完成的。
 其基本思想是这样的:首先捕获该网页的第一个片段,然后滚动条滑道下一页,继续捕获下一个片段,然后这个片段组合到一个目标位图中,就像缝合起来。这个过程会循环进行,直到最后一个片段捕获完毕,缝合到目标位图中去。
         当然我们还可能遇到这样一种情况,就是网页比屏幕要宽,这是我们的程序仍然是先捕获第一个片段,然后浏览器滚动条水平移动,捕获剩余的部分,然后缝接起来,然后再进行上述的步骤。
捕获网页为图像//Get bitmap to hold screen fragment.
捕获网页为图像
 Bitmap bm = new Bitmap(screenWidth, screenHeight, 
捕获网页为图像    System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
捕获网页为图像 
捕获网页为图像 
//Create a target bitmap to draw into.
捕获网页为图像
 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
 }

相关文章:

  • 2022-12-23
  • 2022-01-13
  • 2022-03-05
  • 2021-08-04
  • 2021-08-16
  • 2022-12-23
  • 2021-06-17
猜你喜欢
  • 2021-12-05
  • 2021-08-08
  • 2021-06-13
  • 2021-06-20
  • 2022-12-23
  • 2022-12-23
  • 2021-12-09
相关资源
相似解决方案