delphi - 图像从 TWebBrowser 到 TPicture

标签 delphi winapi rendering delphi-xe2 twebbrowser

如何将已在 TWebBrowser 中下载的图像获取到 TPicture,而不将其复制到剪贴板或查找缓存内容。

最佳答案

好的,我用最后一个答案制作了样本:

首先使用此函数通过 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;

可以使用后:

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;

不要忘记“MSHTML”单元;

关于delphi - 图像从 TWebBrowser 到 TPicture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13444041/

相关文章:

delphi - 如何检查一个类是否实现了一个接口(interface),并尊重超集?

c - C 和 D 中相同的_exact_代码会给出不同的结果——为什么?

html - 在 React 中使用 dangerouslySetInnerHTML 时防止 html 在 css 之前加载

javascript - 在 </body> 标签进入 DOM 树之前,浏览器如何构建渲染树?

javascript - Vue : reinitiating Vue after fetching new components and inserting them into HTML

ios - Delphi iOS模拟器错误信息 'Session ended'

delphi - Delphi 应用程序什么时候对于单个 EXE 来说太大?

windows - 当回收站已满时,ShFileOperation 会做什么?

c++ - 我需要定时器每 25fps 显示图像缓冲区 (C++ win32)

c# - 如何处理表格标题右键单击