• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

CapturingaScreenShotofaTWebBrowserContent(WebPage)inDelphi

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

The TWebBrowser Delphi control provides access to the Web browser functionality from your Delphi apps - to allow you to create a customized Web browsing application or to add Internet, file and network browsing, document viewing, and data downloading capabilities to your applications.

Web Page Screen Shot

You might need to programmatically grab the screen shot of the current page loaded in the web browser control.

A screen shot (screen capture) is a copy of the screen's contents that can be saved as a graphics file.
Web Browser screen shot is a graphics copy of the content on the web browser control - usually a web page (document).

The custom function WebBrowserScreenShot will capture the contents of a TWebBrower's client area into a JPEG image and save it to a specified file.

 uses ActiveX;

procedure WebBrowserScreenShot(const wb: TWebBrowser; const fileName: TFileName) ;
var
  viewObject : IViewObject;
  r : TRect;
  bitmap : TBitmap;
begin
  if wb.Document <> nil then
  begin
    wb.Document.QueryInterface(IViewObject, viewObject) ;
    if Assigned(viewObject) then
    try
      bitmap := TBitmap.Create;
      try
        r := Rect(0, 0, wb.Width, wb.Height) ;

        bitmap.Height := wb.Height;
        bitmap.Width := wb.Width;

        viewObject.Draw(DVASPECT_CONTENT, 1, nil, nil, Application.Handle, bitmap.Canvas.Handle, @r, nil, nil, 0) ;

        with TJPEGImage.Create do
        try
          Assign(bitmap) ;
          SaveToFile(fileName) ;
        finally
          Free;
        end;
      finally
        bitmap.Free;
      end;
    finally
      viewObject._Release;
    end;
  end;
end;
Note: JPEG images are smaller when compared to BMPs - this is why the BMP object is converted to a JPG before saving to the disk.

A sample usage: navigate to a web site in the form's OnCreate event, take the screen shot in the webbrowser's OnNavigateComplete2 event:

 procedure TForm1.FormCreate(Sender: TObject) ;
begin
  WebBrowser1.Navigate('http://delphi.about.com') ;
end;

procedure TForm1.WebBrowser1NavigateComplete2(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant) ;
begin
  WebBrowserScreenShot(WebBrowser1,'c:\WebBrowserImage.jpg') ;
end;
Note: the code above saves the "http://delphi.about.com" site's screen shot to a file named WebBrowserImage.jpg on the C drive.

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap