Category Archives: Delphi

[Delphi] Capturing a Screen Shot of Web page using TWebBrowser

[Delphi] Capturing a Screen Shot of Web page using TWebBrowser

TWebBrowser is not only for Webpage visiting, full page captures and save as Jpg is available.

The program shows below is TWebbrowser to visit a website and capture the screen to save to JPG:

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;

RefLink : http://delphi.about.com/od/vclusing/a/wb_scren_shot.htm