gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:19:32 27 4
gpt4 key购买 nike

我正在尝试制作一个程序,该程序可以截取网站上加载的各个区域的屏幕截图 TWebBrowser零件。

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

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

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

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

如果有人至少能给我一个解决这个问题的方向,我将不胜感激。

谢谢

最佳答案

我建议使用 IHTMLElementRender而是 HTML Elemwnt 的界面。您可以轻松找到光标下的元素并将其渲染为位图。

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

关于delphi - 如何在 TWebBrowser 中捕获页面区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5404099/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com