DelphiFAQ Home Search:

How to make a screenshot of the view of a TWebBrowser

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

Question:

I need to log the contents of a TWebBrowser object, I'd like to save it as a GIF file. How can I do that?

Answer:

The code below will capture the contents of a TWebBrower's client area into a bitmap and save it to a specified file. Depending on the nature of your logged sites, a GIF or PNG image will indeed save substantial disk space.

To support PNG format, I recommend to install this free TPNGImage component:

http://pngdelphi.sourceforge.net/

Assign the TBitmap to the TPNGObject and use the SaveToFile() method as shown in the sample code below. You'll probably save 90% disk space.

program Dummy;

uses
  PNGImage, // for the best PNG support
  ActiveX;

procedure TForm1.SaveWebBrowserScreenshot(wb: TWebBrowser; FileName: TFileName);
var
  ViewObject: IViewObject;
  rec: TRect;
  b: TBitmap;
begin { TForm1.SaveWebBrowserScreenshot }
  if wb.Document<>nil then
  begin
    wb.Document.QueryInterface(IViewObject, ViewObject);
    if ViewObject<>nil then
      try
        b := TBitmap.Create;
        try
          rec := Rect(0, 0, wb.Width, wb.Height);
          b.Height := wb.Height;
          b.Width := wb.Width;
          ViewObject.Draw(DVASPECT_CONTENT, 1, nil, nil, Self.Handle, b.Canvas.
                          Handle, @rec, nil, nil, 0);
          // use the following line to save it as a BMP image file
          // b.SaveToFile(aFileName);

          // use the following line to save it as a PNG image file
          with TPngObject.Create do
          begin
            Assign(b);
            SaveToFile(aFileName);
            Free;
          end;
        finally
          b.Free;
        end; { try }

      finally
        ViewObject._Release;
      end; { try }
  end; { wb.Document<>nil }
end; { TForm1.SaveWebBrowserScreenshot }

begin
  ..
end.

Comments:

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Los Angeles, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.