Windows with Delphi Windows API (94) Windows Filesystem (41) Windows Forms (69) Windows Graphics (38)
Exchange Links About this site Links to us 
|
How to make a screenshot of the view of a TWebBrowser
This 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,
ActiveX;
procedure TForm1.SaveWebBrowserScreenshot(wb: TWebBrowser; FileName: TFileName);
var
ViewObject: IViewObject;
rec: TRect;
b: TBitmap;
begin
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);
with TPngObject.Create do
begin
Assign(b);
SaveToFile(aFileName);
Free;
end;
finally
b.Free;
end;
finally
ViewObject._Release;
end;
end;
end;
begin
..
end. | |  | |  |
Comments:
|