DelphiFAQ Home Search:

Save a web page from TWebBrowser as a JPEG file

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

Question:

I need to make screenshots of web pages. Can I do that elegantly with TWebBrowser?

Answer:

Have TWebBrowser load the page, then have a view object paint the page onto a TCanvas (of a TBitmap) which you then subsequently resize to your desired thumbnail dimension and save in JPEG format.

The example below does not reduce the picture, thus the JPEGs are about full screen size.

You can download a project which was compiled with Delphi 5 here: HTTPtoJPEG.zip (3 kB).

procedure GenerateJPEGfromBrowser(browser: iWebBrowser2;
                                  jpegFQFilename: string; srcHeight:
                                  integer; srcWidth: integer;
                                  tarHeight: integer; tarWidth: integer);
var
  sourceDrawRect: TRect;
  targetDrawRect: TRect;
  sourceBitmap  : TBitmap;
  targetBitmap  : TBitmap;
  aJPG          : TJPEGImage;
  aViewObject   : IViewObject;
begin { GenerateJPEGfromBrowser }
  sourceBitmap := TBitmap.Create;
  targetBitmap := TBitmap.Create;
  aJPG := TJPEGImage.Create;
  try
    try
      sourceDrawRect := Rect(0, 0, srcWidth, srcHeight);
      sourceBitmap.Width := srcWidth;
      sourceBitmap.Height := srcHeight;

      aViewObject := browser as IViewObject;

      if aViewObject=nil then
        Exit;

      OleCheck(aViewObject.Draw(DVASPECT_CONTENT, 1, nil, nil,
                               Form1.Handle,
                               sourceBitmap.Canvas.Handle,
                               @sourceDrawRect, nil, nil, 0));

      // Resize the src bitmap to the target bitmap
      // Need to make thumbnails instead of full size?
      // set the target size here.. 
      targetDrawRect := Rect(0, 0, tarWidth, tarHeight);
      targetBitmap.Height := tarHeight;
      targetBitmap.Width := tarWidth;
      targetBitmap.Canvas.StretchDraw(targetDrawRect, sourceBitmap);

      // Create a JPEG from the Bitmap and save it
      aJPG.Assign(targetBitmap);

      aJPG.SaveToFile(jpegFQFilename)
    finally
      aJPG.Free;
      sourceBitmap.Free;
      targetBitmap.Free
    end; { try }

  except
    // error handler code
  end; { try }
end; { GenerateJPEGfromBrowser }


procedure TForm1.Button2Click(Sender: TObject);
var
  IDoc1: IHTMLDocument2;
  Web  : iWebBrowser2;
  tmpX,
  tmpY : integer;
begin { TForm1.Button2Click }
  with WebBrowser1 do 
  begin
    Document.QueryInterface(IHTMLDocument2, IDoc1); 
    Web := ControlInterface; 
    tmpX := Height; 
    tmpY := Width; 
    Height := OleObject.Document.ParentWindow.Screen.Height; 
    Width := OleObject.Document.ParentWindow.Screen.Width; 
    GenerateJPEGfromBrowser(Web, '.\test.jpg',
                            Height, Width,
                            Height, Width);
    Height := tmpX;
    Width := tmpY;

    Image1.Picture.LoadFromFile('.\test.jpg')
  end; { with WebBrowser1 }
end; { TForm1.Button2Click } 
You don't like the formatting? Check out SourceCoder then!

Comments:

 

 

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: