четвртак, 9. август 2012.

png screenshoter for delphi

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.

C
C

Нема коментара:

Постави коментар