Taking screenshot of webpage

ST3VO
ST3VO used Ask the Experts™
on
Hi all,

How could I take screenshots of a webpage opened by Firefox and internet explorer for example please.

Been looking everyway and I've seen it been done on the web but I have not clue how I could do it.

Can anyone help please?

thx

St3vo
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I use this:
http://www.faststone.org/FSCaptureDetail.htm
it works for 30 days...but still worth buying afterwards..
Geert GOracle dba
Top Expert 2009

Commented:
this is a other such product
http://www.techsmith.com/snagit/features.asp

Commented:
Delphi.About.com is a great place for hints and tricks of all sorts.

Here is a link to them with code on how to take a screenshot of the active window which could be any browser window.

http://delphi.about.com/od/adptips2006/qt/captureactive.htm

John
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

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;

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://edn.embarcadero.com/') ;
end;

procedure TForm1.WebBrowser1NavigateComplete2(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant) ;
begin
  WebBrowserScreenShot(WebBrowser1,'c:\WebBrowserImage.jpg') ;
end;

Author

Commented:
Hi thiagoblimeira,

I've tested it but it seems to take a screenshot of the background only...or that or itself taking a sshot of a small part of the page as I can only see the background color in the image.

Any ideas why please?

thx

st3vo
Sorry I forgot to mention that the webbrowser.Visible property must be set to False

Test now please

Author

Commented:
Hmmm...still not working for some reason.

Please see code attached

thanks


unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ActiveX, OleCtrls, SHDocVw, ComCtrls, jpeg, StdCtrls, ExtCtrls;
 
type
  TForm1 = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    Firefox: TTabSheet;
    Safari: TTabSheet;
    TabSheet3: TTabSheet;
    TabSheet4: TTabSheet;
    TabSheet5: TTabSheet;
    TabSheet6: TTabSheet;
    TabSheet7: TTabSheet;
    Edit1: TEdit;
    Button1: TButton;
    Panel1: TPanel;
    WebBrowser1: TWebBrowser;
    procedure Button1Click(Sender: TObject);
    procedure WebBrowser1NavigateComplete2(ASender: TObject;
      const pDisp: IDispatch; var URL: OleVariant);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
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;
 
 
procedure TForm1.Button1Click(Sender: TObject);
begin
 
WebBrowser1.Navigate('http://www.google.com/') ;
end;
 
procedure TForm1.WebBrowser1NavigateComplete2(ASender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
begin
//WebBrowser1.Visible := False;
WebBrowserScreenShot(WebBrowser1,'WebBrowserImage.jpg');
//WebBrowser1.Visible := True;
end;
 
end.

Open in new window

one thing I noticed is that only the code doesn't work for google page, if you try a different page it works, do you need to take the screenshot from google's page?
I can't figure out why is this happening, but I find an answer to this.

Author

Commented:
Not here.

I cannot make it work on any page at all.

Tried your link:

WebBrowser1.Navigate('http://edn.embarcadero.com/');

and still not working

Just get a blank image or the background color if the page.

any ideas?

Solved, use the WebBrowser1DocumentComplete event.

procedure TForm2.WebBrowser1DocumentComplete(ASender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
begin
  WebBrowserScreenShot(WebBrowser1,'c:\WebBrowserImage.jpg');
end;

procedure TForm2.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;

procedure TForm2.FormCreate(Sender: TObject);
begin
  WebBrowser1.Navigate('www.experts-exchange.com') ;
end;

Author

Commented:
Thanks thiagoblimeira,

It works now.

I think this is the first step.

I am going to open another question for the rest as it seems to be a hard topic and I need to do this with other browsers and not just tbrowser component.

Thanks a lot for your help!

st3vo

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial