DelphiFAQ Home Search:
General :: Windows :: Programming :: Windows with Delphi :: Windows Graphics
Graphics programming under Windows with Delphi.

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.

Featured Article

How to make a screenshot of the view of a TWebBrowser

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.

Generated 20:03:43 on Oct 17, 2018