【问题标题】:How to capture page regions in TWebBrowser?如何在 TWebBrowser 中捕获页面区域?
【发布时间】:2011-03-24 16:05:16
【问题描述】:

我正在尝试制作一个程序,该程序将在 TWebBrowser 组件中加载的站点上的各个区域进行屏幕截图。

到目前为止,我只找到了“如何制作整个页面的屏幕截图”的解决方案,但我无法让它捕捉特定区域,它只是将页面拉伸到任何方向。

http://www.delphifaq.com/faq/f408.shtml

我一直在使用上面网站中提供的代码。

有没有办法修改代码,让它做我需要的?我试过了,但我失败了。

如果有人至少可以指导我如何解决这个问题,我将不胜感激。

谢谢

【问题讨论】:

    标签: delphi screenshot twebbrowser


    【解决方案1】:

    我建议改用 HTML Elemwnt 的IHTMLElementRender 接口。您可以轻松找到光标下的元素并将其渲染为位图。

    在我的 TWebBrowser descant 中,我是这样实现的:

    function TWebBrowserIBMA.ElementAsBitmap(pElement : IHTMLElement2) : TBitmap;
    var
      pRender       : IHTMLElementRender;
      oBmpPart      : TBitmap;
      nClientWidth  : Integer;
      nClientHeight : Integer;
      nX            : Integer;
      nLastX        : Integer;
      bDoneX        : Boolean;
      nY            : Integer;
      nLastY        : Integer;
      bDoneY        : Boolean;
    begin
      Result := TBitmap.Create;
    
      try
        Result.Height := pElement.scrollHeight;
        Result.Width  := pElement.scrollWidth;
      except
        exit;
      end;
    
      LockWindowUpdate(Handle);
    
      if (pElement.QueryInterface(IID_IHTMLElementRender, pRender) = S_OK) then begin
        try
          oBmpPart        := TBitmap.Create;
          oBmpPart.Width  := pElement.scrollWidth;
          oBmpPart.Height := pElement.scrollHeight;
          nClientWidth    := pElement.clientWidth;
          nClientHeight   := pElement.clientHeight;
    
          try
            nX      := pElement.scrollWidth; 
            nLastX  := -1;
            bDoneX  := false;
    
            while not bDoneX do begin
              pElement.scrollLeft := nX;
              nX := pElement.scrollLeft;
              if nLastX = -1 then begin
                nLastX := nX + nClientWidth;
              end;
              nY     := pElement.scrollHeight;
              nLastY := (-1);
              bDoneY := false;
    
              while not bDoneY do begin
                pElement.scrollTop := nY;
                nY := pElement.scrollTop;
    
                if nLastY = -1 then begin
                  nLastY := nY + nClientHeight;
                end;
    
                if (pRender.DrawToDC(oBmpPart.Canvas.Handle) = S_OK) then begin
                  BitBlt(Result.Canvas.Handle, nX, nY, nLastX-nX, nLastY-nY, oBmpPart.Canvas.Handle, 2, 2,SRCCOPY);
                end;
    
                bDoneY  := (nY = 0);
                nLastY  := nY;
                Dec(nY, nClientHeight-4);  
              end;
    
              bDoneX  := (nX = 0);
              nLastX  := nX;
              Dec(nX, (nClientWidth-4));
            end;
          finally
            FreeAndNil(oBmpPart);
          end;
        finally
          pRender := nil;
        end;
      end;
    
      LockWindowUpdate(0);
    end;
    

    【讨论】:

      【解决方案2】:

      你可以使用 sourceBitmap.Canvas.CopyRect

      【讨论】:

        【解决方案3】:

        您是否尝试过将sourceDrawRect 设置为一个顶部和左侧为负的矩形,并且右侧和底部超出您让视图对象绘制到的位图的宽度和高度,以便您想要的区域落在该位图上?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-21
          • 1970-01-01
          相关资源
          最近更新 更多