DelphiFAQ Home Search:

How to make a screenshot of the view of a TWebBrowser

 

comments8 comments. Current rating: 3 stars (8 votes). Leave comments and/ or 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:

2020-06-28, 14:11:45
Rico  
rating
viagra cheap
2020-07-01, 10:59:59
Ericka  
rating
viagra online no prescriptions usa
2020-07-02, 02:53:34
Georgia  
rating
clomid 100 mg
2020-07-05, 17:22:03
Paulina from Ukraine  
rating
safe viagra on the web
2020-07-10, 08:28:31
Celina  
rating
when does ventolin hfa go generic
2020-07-19, 07:34:11
Rosaline from Greece  
rating
how long does cialis last
2020-07-26, 18:27:22
Shawnee  
rating
cost of naltrexone at walmart
2020-08-05, 00:55:28
Kira from Schonenwerd, Switzerland  
rating
interactions for lumigan

 

 

NEW: Optional: Register   Login
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, or post under a registered account.
 

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:

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.

photo Add a picture: