簡體   English   中英

如何在TWebBrowser中捕獲頁面區域?

[英]How to capture page regions in TWebBrowser?

我正在嘗試制作一個程序,該程序將對TWebBrowser組件中加載的站點上各個區域進行截圖。

到目前為止,我只找到了“如何制作整個頁面的屏幕截圖”的解決方案,但我只是無法使其捕獲特定區域而已,它只是將頁面向任意方向延伸。

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

我一直在使用上面網站中提供的代碼。

有沒有一種方法可以修改代碼,所以它將滿足我的需要? 我嘗試了但是失敗了。

如果有人至少可以給我指導如何解決這個問題,我將不勝感激。

謝謝

我建議改用HTML Elemwnt的IHTMLElementRender接口。 您可以輕松地在光標下找到該元素並將其呈現為位圖。

在我的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