1

How to get an image that has been downloaded in TWebBrowser to a TPicture without copying it to clipboard or looking in to cache contents.

5
  • If you just want to download an image (or any other file) from the web, that's very easy. (Much easier than using a TWebBrowser). But I guess you have your reasons? Nov 18, 2012 at 20:20
  • @AndreasRejbrand i updated my question. I found a c# solution. But would need to someone to translate.
    – user1647411
    Nov 18, 2012 at 20:20
  • DrawToDC is deprecated, see MSDN DOC. Also this SO question: How to render WebBrowser to device context?.
    – LU RD
    Nov 18, 2012 at 20:50
  • @LURD not quite the same question he want to render whole page.
    – user1647411
    Nov 18, 2012 at 20:56
  • That question illustrates the problem with the deprecated DrawToDC.
    – LU RD
    Nov 18, 2012 at 20:58

1 Answer 1

8

ok i made sample with last answer to you :

fisrt get image with this function by Id :

function GetImgElementById(const Doc: IDispatch; const id : string): IHTMLImgElement;
var
  Document: IHTMLDocument2;     // IHTMLDocument2 interface of Doc
  Body: IHTMLElement2;          // document body element
  Tags: IHTMLElementCollection; // all tags in document body
  Tag: IHTMLElement;            // a tag in document body
  I: Integer;                   // loops thru tags in document body
begin

  Result :=nil ;
  // Check for valid document: require IHTMLDocument2 interface to it
  if not Supports(Doc, IHTMLDocument2, Document) then
    raise Exception.Create('Invalid HTML document');
  // Check for valid body element: require IHTMLElement2 interface to it
  if not Supports(Document.body, IHTMLElement2, Body) then
    raise Exception.Create('Can''t find <body> element');
  // Get all tags in body element ('*' => any tag name)
  Tags := Body.getElementsByTagName('img');
  // Scan through all tags in body
  for I := 0 to Pred(Tags.length) do
  begin
    // Get reference to a tag
    Tag := Tags.item(I, EmptyParam) as IHTMLElement;
    // Check tag's id and return it if id matches
    if AnsiSameText(Tag.id, id) then
    begin
      Result := Tag as IHTMLImgElement ;
      Break;
    end;
  end;
end;

after you can use it :

var
  img : IHTMLImgElement ;
  rnd : IHTMLElementRender ;
begin
  //
  img := GetImgElementById(wb1.Document,'imgid');
  // img1 is TImage
  img1.Height := img.height ;
  img1.Width := img.width ;
  rnd := img as IHTMLElementRender ;
  rnd.DrawToDC(img1.Canvas.Handle);
end;

dont forget "MSHTML" unit ;

4
  • Is it possible to modify the function based on src attribute? tag=AnsiContainsStr('imagehostname.com') <img id="Id" style="display: inline; width: 218px; height: 48px;" src="imagehostname.com?u=235235235235235">
    – user1647411
    Nov 18, 2012 at 21:10
  • you can get info with Tag.getAttribute('src') in if function
    – A1Gard
    Nov 18, 2012 at 21:21
  • Can this be used with the indy IdHttp? Jun 15, 2013 at 9:51
  • No, you can't use direct this way , you must passing result to TWebBrowser .
    – A1Gard
    Jun 20, 2013 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.