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

发布于 2024-10-26 03:29:10 字数 374 浏览 6 评论 0原文

我正在尝试制作一个程序,该程序可以对加载到 TWebBrowser 组件内的网站上的各个区域进行屏幕截图。

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

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

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

有没有办法修改代码,使其能够满足我的需要?我尝试过,但失败了。

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

谢谢

I am trying to make a program which would make screenshots of various areas on the site that is loaded inside TWebBrowser component.

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

-黛色若梦 2024-11-02 03:29:10

我建议改用 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;

I recommend to use the IHTMLElementRender interface of the HTML Elemwnt instead. You can easily find the element under the cursor and render this to a bitmap.

In my TWebBrowser descant I implemented it like this:

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;
沧桑㈠ 2024-11-02 03:29:10

您可以使用 sourceBitmap.Canvas.CopyRect

You can use sourceBitmap.Canvas.CopyRect

分分钟 2024-11-02 03:29:10

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

Have you tried setting sourceDrawRect to a rectangle with negative top and left, and a right and bottom past the width and height of the bitmap you let the viewobject draw onto, so that the region you want falls onto this bitmap?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文