简体   繁体   English

如何在TWebBrowser中捕获页面区域?

[英]How to capture page regions in TWebBrowser?

I am trying to make a program which would make screenshots of various areas on the site that is loaded inside TWebBrowser component. 我正在尝试制作一个程序,该程序将对TWebBrowser组件中加载的站点上各个区域进行截图。

So far i have found only solutions to 'how to make screenshot of the whole page', but i just could not make it work to capture specific region, it just stretches the page in whatever direction. 到目前为止,我只找到了“如何制作整个页面的屏幕截图”的解决方案,但我只是无法使其捕获特定区域而已,它只是将页面向任意方向延伸。

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

I have been using the code presented in the site above. 我一直在使用上面网站中提供的代码。

Is there a way to modify the code so it would do what i need? 有没有一种方法可以修改代码,所以它将满足我的需要? I tried but i failed. 我尝试了但是失败了。

I would appreciate if anybody could at least give me a direction to how to solve this. 如果有人至少可以给我指导如何解决这个问题,我将不胜感激。

Thanks 谢谢

I recommend to use the IHTMLElementRender interface of the HTML Elemwnt instead. 我建议改用HTML Elemwnt的IHTMLElementRender接口。 You can easily find the element under the cursor and render this to a bitmap. 您可以轻松地在光标下找到该元素并将其呈现为位图。

In my TWebBrowser descant I implemented it like this: 在我的TWebBrowser取消扫描中,我是这样实现的:

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;

您可以使用sourceBitmap.Canvas.CopyRect

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM