¿Cómo capturar regiones de página en TWebBrowser?
-
28-10-2019 - |
Pregunta
Estoy intentando crear un programa que haga capturas de pantalla de varias áreas del sitio que se cargan dentro del componente TWebBrowser
.
Hasta ahora, solo he encontrado soluciones para 'cómo hacer una captura de pantalla de toda la página', pero simplemente no pude hacer que funcione para capturar una región específica, simplemente estira la página en cualquier dirección.
http://www.delphifaq.com/faq/f408.shtml
He estado usando el código presentado en el sitio anterior.
¿Hay alguna forma de modificar el código para que haga lo que necesito?Lo intenté pero fallé.
Le agradecería si alguien pudiera al menos darme una dirección sobre cómo resolver esto.
Gracias
Solución
En su lugar, recomiendo utilizar la interfaz IHTMLElementRender
de HTML Elemwnt.Puede encontrar fácilmente el elemento debajo del cursor y convertirlo en un mapa de bits.
En mi descant de TWebBrowser lo implementé así:
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;
Otros consejos
Puede utilizar sourceBitmap.Canvas.CopyRect
¿Ha intentado establecer sourceDrawRect
en un rectángulo con una parte superior e izquierda negativa, y una derecha y una parte inferior más allá del ancho y la altura del mapa de bits en el que dejó que se dibujara el objeto de la vista, de modo que la región que desea caiga en este mapa de bits?p>