zoukankan      html  css  js  c++  java
  • Capturing a Screen Shot of a TWebBrowser Content (Web Page) in Delphi

    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.
  • 相关阅读:
    巨蟒django之权限9:前端展示修改删除合并&&权限展示
    巨蟒django之权限8:排序&&菜单展开权限归属
    巨蟒django之权限7:动态生成一级&&二级菜单
    巨蟒django之权限6: 权限控制表设计&&登录权限
    巨蟒django之CRM5 学习记录&&课程记录&&班级管理&&私户的数量上限
    巨蟒django之CRM4 一些小功能
    巨蟒django之CRM3 添加和编辑客户&&公户和私户的展示和转换
    巨蟒django之CRM2 展示客户列表&&分页
    巨蟒django之CRM1 需求分析&&表结构设计&&注册登录验证
    sysfs接口函数到建立_DEVICE_ATTR
  • 原文地址:https://www.cnblogs.com/iihe602/p/1777693.html
Copyright © 2011-2022 走看看