如何使用iviewobject的draw方法把当前ie页面转成一个图片?我在自己程序获取twebbrowser没问题,可是外部ie就是不行,请高手指教! ( 积

  • 主题发起人 主题发起人 jsres
  • 开始时间 开始时间
J

jsres

Unregistered / Unconfirmed
GUEST, unregistred user!
如何使用iviewobject的draw方法把当前ie页面转成一个图片?我在自己程序获取twebbrowser没问题,可是外部ie就是不行,请高手指教! ( 积分: 200 )<br />我想把当前ie页面转成一个图片,可以使用iviewobject接口的draw方法
示例程序如下
procedure TForm1.Button1Click(Sender: TObject);
var
ViewObject: IViewObject;
sourceDrawRect: TRect;
begin
if Webbrowser1.Document &lt;&gt; nil then
try
webbrowser1.Document.QueryInterface(IViewObject, ViewObject);
if ViewObject &lt;&gt; nil then
try
sourceDrawRect := Rect(0, 0, Image1.Width, Image1.Height);
ViewObject.Draw(DVASPECT_CONTENT, 1, nil, nil, Self.Handle,
image1.Canvas.Handle, @sourceDrawRect, nil, nil, 0);
finally
end;
except
end;
end;

因为webbrowser1是自己程序里的,所以上面程序工作正常.
可是我想获得一个外部IE页面的图片.
我先获得外部IE的句柄,然后转成iwebbrowser2接口,接着使用上面的方法(把webbrowser1换替换一下)
可是draw出来的图片是空白的

为什么呢?请大家指教!我急死了呀!
 
我想把当前ie页面转成一个图片,可以使用iviewobject接口的draw方法
示例程序如下
procedure TForm1.Button1Click(Sender: TObject);
var
ViewObject: IViewObject;
sourceDrawRect: TRect;
begin
if Webbrowser1.Document &lt;&gt; nil then
try
webbrowser1.Document.QueryInterface(IViewObject, ViewObject);
if ViewObject &lt;&gt; nil then
try
sourceDrawRect := Rect(0, 0, Image1.Width, Image1.Height);
ViewObject.Draw(DVASPECT_CONTENT, 1, nil, nil, Self.Handle,
image1.Canvas.Handle, @sourceDrawRect, nil, nil, 0);
finally
end;
except
end;
end;

因为webbrowser1是自己程序里的,所以上面程序工作正常.
可是我想获得一个外部IE页面的图片.
我先获得外部IE的句柄,然后转成iwebbrowser2接口,接着使用上面的方法(把webbrowser1换替换一下)
可是draw出来的图片是空白的

为什么呢?请大家指教!我急死了呀!
 
这是我在《资料收集库》里的完整代码。如果好用,记得向朋友介绍《资料收集库》哦。

代码:
void __fastcall TFlyingForm::GetWebFullPic(LPDISPATCH WebDoc,HWND hWnd,
                                        AnsiString strTitle, AnsiString strUrl)
{
    //TODO: 取得鼠标指向的页面的全部图象。
    AnsiString FullPicTempPath=GetDocBookTempPath()+&quot;WebFullPic.jpg&quot;;
    //IViewObject *ViewObject;
    ::IViewObject *ViewObject=0;
    IHTMLDocument2 *pDocument ;
    IHTMLWindow2   *pHtmlWin;
    IHTMLElement   *pHtmlBodyElem;
    RECTL sourceDrawRect;
    HBITMAP pDest,pSour;
    HRESULT hr ;
    int i,m;
    int iScrollHeight,iScrollWidth;
    VARIANT vScrollHeight,vScrollWidth;
    VARIANT vOffsetHeight,vOffsetWidth;
    VARIANT vScrollLeft,vScrollTop;

    //获得IHTMLDocument2接口
    hr = WebDoc-&gt;QueryInterface(IID_IHTMLDocument2,(void**)&amp;pDocument) ;
    if(SUCCEEDED(hr) )
    {
        wchar_t *url;
        pDocument-&gt;get_URL(&amp;url);
        //hr=pDocument-&gt;QueryInterface(IID_IViewObject, (void**)&amp;ViewObject);
        hr=pDocument-&gt;QueryInterface(::IID_IViewObject,reinterpret_cast&lt;void**&gt;(&amp;ViewObject));

        if(SUCCEEDED(hr) )
        {
           Graphics :: TBitmap *HtmlBmp = new  Graphics :: TBitmap();
           HtmlBmp-&gt;PixelFormat= pf24bit;
           try
           {
              pDocument-&gt;get_parentWindow(&amp;pHtmlWin);
              pHtmlWin-&gt;scroll(0,0);
              pDocument-&gt;get_body(&amp;pHtmlBodyElem);
              pHtmlBodyElem-&gt;getAttribute(WideString(&quot;scrollHeight&quot;),0,&amp;vScrollHeight);
              pHtmlBodyElem-&gt;getAttribute(WideString(&quot;scrollwidth&quot;),0,&amp;vScrollWidth);
              iScrollHeight = vScrollHeight.lVal;
              iScrollWidth =  vScrollWidth.lVal;
              HtmlBmp-&gt;Height = iScrollHeight ;
              HtmlBmp-&gt;Width = iScrollWidth   ;
              pDest = HtmlBmp-&gt;Canvas-&gt;Handle;
              pHtmlBodyElem-&gt;getAttribute(WideString(&quot;offsetHeight&quot;),0,&amp;vOffsetHeight);
              pHtmlBodyElem-&gt;getAttribute(WideString(&quot;offsetwidth&quot;),0,&amp;vOffsetWidth);
              m = iScrollHeight / ( vOffsetHeight.lVal - 20);  /*Trunc()*/
              i = iScrollWidth  / ( vOffsetWidth.lVal - 20);

              for(int j=0;j&lt;i;j++)
              {
                  for(int k=0;k&lt;=m;k++)
                  {
                    Graphics :: TBitmap *tempBmp = new  Graphics :: TBitmap();
                    int iLeft=0;
                    pHtmlBodyElem-&gt;getAttribute(WideString(&quot;offsetHeight&quot;),0,&amp;vOffsetHeight);
                    pHtmlBodyElem-&gt;getAttribute(WideString(&quot;offsetWidth&quot;),0,&amp;vOffsetWidth);
                    tempBmp-&gt;Height = vOffsetHeight.lVal;
                    tempBmp-&gt;Width =  vOffsetWidth.lVal;

                    pSour = tempBmp-&gt;Canvas-&gt;Handle;
                    sourceDrawRect.top=0;
                    sourceDrawRect.left=0;
                    sourceDrawRect.right=tempBmp-&gt;Width;
                    sourceDrawRect.bottom=tempBmp-&gt;Height;
                    
                    TRect HtmlRect;
                    HDC HtmlDC=GetWindowDC(hWnd);
                    TCanvas *BCanvas = new TCanvas();

                    HtmlRect.Left=0;
                    HtmlRect.top=0;
                    int a=HtmlRect.Width();
                    HtmlRect.Right=tempBmp-&gt;Width;
                    HtmlRect.Bottom= tempBmp-&gt;Height;
                    BCanvas-&gt;Handle=HtmlDC;
                    tempBmp-&gt;Canvas-&gt;CopyRect(HtmlRect,BCanvas,HtmlRect);
                    delete BCanvas;
                    ReleaseDC(HtmlDC,0);
                    /*
                     //会出错。没法了。只好不用了。
                    OleCheck(ViewObject-&gt;Draw(DVASPECT_CONTENT, 1, NULL,NULL,Handle,
                             tempBmp-&gt;Canvas-&gt;Handle,&amp;sourceDrawRect, NULL, NULL, 0));
                    */
                    pHtmlBodyElem-&gt;getAttribute(WideString(&quot;scrollLeft&quot;),0,&amp;vScrollLeft);
                    pHtmlBodyElem-&gt;getAttribute(WideString(&quot;Scrolltop&quot;),0,&amp;vScrollTop);

                    BitBlt(pDest, vScrollLeft.lVal , vScrollTop.lVal,
                           tempBmp-&gt;Width, tempBmp-&gt;Height, pSour, 2, 2, SRCCOPY);

                    pHtmlBodyElem-&gt;getAttribute(WideString(&quot;offsetHeight&quot;),0,&amp;vOffsetHeight);
                    pHtmlBodyElem-&gt;getAttribute(WideString(&quot;Scrolltop&quot;),0,&amp;vScrollTop);
                    iLeft= vOffsetHeight.lVal + vScrollTop.lVal
                           - GetSystemMetrics(SM_CXVSCROLL) - 24;
                    pHtmlBodyElem-&gt;getAttribute(WideString(&quot;scrollLeft&quot;),0,&amp;vScrollLeft);
                    pHtmlWin-&gt;scroll(vScrollLeft.lVal ,iLeft );
                                        
                    delete tempBmp;
                    tempBmp=NULL;
                  }// end of for(int k=0;k&lt;=m;k++)
                  int iLeftSize=0;
                  pHtmlBodyElem-&gt;getAttribute(WideString(&quot;offsetWidth&quot;),0,&amp;vOffsetWidth) ;
                  pHtmlBodyElem-&gt;getAttribute(WideString(&quot;scrollLeft&quot;),0,&amp;vScrollLeft);
                  iLeftSize = vOffsetWidth.lVal + vScrollLeft.lVal
                              - GetSystemMetrics(SM_CXVSCROLL) - 24;
                  pHtmlWin-&gt;scroll(iLeftSize,0);
              } //end of for(j=0;j&lt;i;j++)
              TJPEGImage * HtmlJpg = new TJPEGImage();
              HtmlJpg-&gt;CompressionQuality=80;
              HtmlJpg-&gt;Assign(HtmlBmp);
              HtmlJpg-&gt;SaveToFile(FullPicTempPath);
              delete HtmlJpg;
              HtmlJpg=NULL;
           }__finally
           {
                ViewObject-&gt;Release();
                delete HtmlBmp;
                HtmlBmp=NULL;
           }
        }// end of   if(SUCCEEDED(hr) )
        pDocument-&gt;Release() ;
    }// end of   if(SUCCEEDED(hr) )

}
//---------------------------------------------------------------------------
 
后退
顶部