网页中的静态图片加载后会保存在IE缓存区内,所以只要获取图片URL在IE缓存区中对应的本地文件名,就可以使用Win32API函数:copyFile拷贝到指定的文件名就可以了,获取URL对应本地文件名的函数如下:
uses WinInet
function GetLocalFileNameFromIECache(url:string; var LocalFileName:string):DWORD;
var
D: Cardinal;
T: PInternetCacheEntryInfo;
begin
result := S_OK;
D := 0;
T:=nil;
GetUrlCacheEntryInfo(PChar(Url),T^,D);
Getmem(T,D);
try
if (GetUrlCacheEntryInfo(PChar(Url),D)) then
LocalFileName:=T^.lpszLocalFileName
else
Result := GetLastError;
finally
Freemem(T,D);
end;
end;
网页中动态生成的图片有些并不保存在IE缓存区中,这种图片只能使用TWebbrowser控件获取,下面的代码将网页中的第一幅图片复制到Windows剪贴板,并用Image1控件显示:
uses MsHtml,ActiveX,ClipBrd
procedure TForm1.Button1Click(Sender: TObject);
var
iIndex: Integer;
Rang:IHTMLControlRange;
ImgSel: IHTMLControlElement;
begin
iIndex := 0; //所需的图片在网页中出现的顺序
Rang := ((IHTMLDocument2(Webbrowser1.Document).body as HTMLBody).createControlRange) as IHTMLControlRange;
ImgSel := IHTMLDocument2(Webbrowser1.Document).images.item(iIndex,EmptyParam)as IHTMLControlElement;
Rang.add(ImgSel);
Rang.execCommand('copy',False,0);
Image1.Picture.Assign(ClipBoard);
end;initialization
OleInitialize(nil);
finalization
OleUninitialize;end.
3. 将Image1控件中的图像保存为BMP文件
procedure TForm1.Button2Click(Sender: TObject);
var
Bmp: TBitmap;
begin
if not SaveDialog1.Execute then Exit;
bmp := TBitmap.Create;
bmp.Width := Image1.Width;
bmp.Height := Image1.Height;
bmp.Canvas.Draw(0, etoFile(SaveDialog1.FileName);
bmp.Free;
end