FAQ VCL
Компоненты\WebBrowser

:: Меню ::
:: На главную ::
:: FAQ ::
:: Заметки ::
:: Практика ::
:: Win API ::
:: Проекты ::
:: Скачать ::
:: Секреты ::
:: Ссылки ::

:: Сервис ::
:: Написать ::

:: MVP ::

:: RSS ::

Яндекс.Метрика

Как сохранить все картинки из TWebBrowser?

uses
  UrlMon;

procedure TForm1.FormCreate(Sender: TObject);
begin
   WebBrowser1.Navigate('http://decoding.dax.ru');
end;

function DownloadFile(SourceFile, DestFile: string): Boolean;
begin
   try
      Result := UrlDownloadToFile(nil, PChar(SourceFile), PChar(DestFile), 0, nil) = 0;
   except
      Result := False;
   end;
end;

procedure SaveAllImagesToDir(const DirPath: string; WB: TWebBrowser);
var
  i, j: Integer;
  Source, dest, ext: string;
begin
   for i := 0 to WB.OleObject.Document.Images.Length - 1 do
   begin
      Source := WB.OleObject.Document.Images.Item(i).Src;
      j := LastDelimiter('.', Source);
      ext := UpperCase(Copy(Source, j + 1, Length(Source)));
      if (ext = 'GIF') or (ext = 'JPG') or (ext = 'JPEG') or (ext = 'BMP') or (ext = 'PNG') then
      begin
         j  := LastDelimiter('/', Source);
         dest := DirPath + Copy(Source, j + 1, Length(Source));
         DownloadFile(Source, dest);
      end;
   end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   if not VarIsClear(WebBrowser1.OleObject.Document) then
   begin
      SaveAllImagesToDir('d:\tmp\', WebBrowser1);
   end;
end;

При использовании материала - ссылка на сайт обязательна