Image from TWebBrowser to TPicture

孤者浪人 提交于 2019-11-28 06:19:05

问题


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.


回答1:


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 ;



来源:https://stackoverflow.com/questions/13444041/image-from-twebbrowser-to-tpicture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!