Link to home
Start Free TrialLog in
Avatar of ST3VO
ST3VOFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Taking screenshot of webpage

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
Avatar of Peter Hart
Peter Hart
Flag of United Kingdom of Great Britain and Northern Ireland image

I use this:
http://www.faststone.org/FSCaptureDetail.htm
it works for 30 days...but still worth buying afterwards..
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
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;
Avatar of ST3VO

ASKER

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
Avatar of ST3VO

ASKER

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.
Avatar of ST3VO

ASKER

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?

ASKER CERTIFIED SOLUTION
Avatar of thiagoblimeira
thiagoblimeira
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ST3VO

ASKER

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