delphi

how to save everything in the webbrowser as Image

knoen 2014. 1. 26. 13:13

I would like to save the content of a webbrowser as image. is that possible?

the purpose is to create a screenshot of a page and then viewed it to other application as images.

anyone can help?

share|improve this question
add comment

Here's an article which provides an example:

uses ActiveX;
 procedure WebBrowserScreenShot(const wb: TWebBrowser; const fileName: TFileName) ;
 var
   viewObject : IViewObject;
   r : TRect;
   bitmap : TBitmap;
 begin
   if wb.Document <> nil then
   begin
     wb.Document.QueryInterface(IViewObject, viewObject) ;
     if Assigned(viewObject) then
     try
       bitmap := TBitmap.Create;
       try
         r := Rect(0, 0, wb.Width, wb.Height) ;
         bitmap.Height := wb.Height;
         bitmap.Width := wb.Width;
         viewObject.Draw(DVASPECT_CONTENT, 1, nil, nil, Application.Handle, bitmap.Canvas.Handle, @r, nil, nil, 0) ;
         with TJPEGImage.Create do
         try
           Assign(bitmap) ;
           SaveToFile(fileName) ;
         finally
           Free;
         end;
       finally
         bitmap.Free;
       end;
     finally
       viewObject._Release;
     end;
   end;
 end; 

and use like this:

procedure TForm1.FormCreate(Sender: TObject) ;
 begin
   WebBrowser1.Navigate('http://delphi.about.com') ;
 end;
 procedure TForm1.WebBrowser1NavigateComplete2(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant) ;
 begin
   WebBrowserScreenShot(WebBrowser1,'c:\WebBrowserImage.jpg') ;
 end; 
share|improve this answer