分享

提取WebBrowser里的图片 delphi

 sumstars 2014-11-19

有一个Form1,里面有image1和webBrowser1,我用WebBrowser1连接到一个有验证码的网页,想把里面的图片提取到image1里,应该怎么做?

问题补充:

我指的是图片。例如:http://china.alibaba.com/member/join.htm验证码的地址是: http://checkcode.china.alibaba.com/service/checkcode?sessionID=e9EgR8RbEt2F8k68tQGjCKJ9NZnf8%24cS那里的图片是每次都变化的。我需要的是把WebBrowser里显示的验证码加载到TImage的对象里。下面的图像如果看不清,就看这个链接:http://hiphotos.baidu.com/neek/pic/item/a4c5f0d3804a83fca9ec9ac3.jpeg 

最佳答案:

procedure DomImg2Image(src:string;wb:TWebBrowser;img:TImage);
var
  i:Integer;
  rang:IHTMLControlRange;
begin
  for i:=0 to IHTMLDocument2(wb.Document).images.length-1 do
    if Pos(src,(IHTMLDocument2(wb.Document).images.item(i,EmptyParam)as
      IHTMLElement).getAttribute('src',0))>0 then
    begin
      rang:=((IHTMLDocument2(wb.Document).body as HTMLBody).createControlRange)as
        IHTMLControlRange;
      rang.add(IHTMLDocument2(wb.Document).images.item(i,EmptyParam)as
        IHTMLControlElement);
      rang.execCommand('Copy',False,0);
      try img.Picture.Assign(ClipBoard)except end;
      break;
    end;
end;
procedure TForm1.WebBrowser1DocumentComplete(Sender:TObject;
const pDisp:IDispatch;var URL:OleVariant);
begin
  DomImg2Image('checkcode?',TWebBrowser(Sender),Image1);
end;
必须先uses ActiveX
initialization
OleInitialize(nil);
finalization
OleUninitialize;

还要加一个单元文件 clipbrd 和 mshtml。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多