3

I need to save an specific image in Twebbrowser, but WITHOUT reloading it ; the image is already loaded. I know how to get the source url of the image and download it, but what i really need is to save the image it's already loaded in the browser. Is there a way ?

Thanks !

delphirules
  • 6,443
  • 17
  • 59
  • 108
  • 2
    i think if it is shown - then it is already in your app section of MSIE cache, so if you would open it directly, it would be fetched from cache, not from server – Arioch 'The Jan 10 '14 at 13:39
  • Yes, this is what i want, save from the IE cache ; i don't know how to do it though. – delphirules Jan 10 '14 at 15:24
  • I've just found a question with the solution :) http://stackoverflow.com/questions/13444041/image-from-twebbrowser-to-tpicture – delphirules Jan 10 '14 at 15:35
  • You've asked how to save an already loaded image, not how to render it to a device context... Yes, there is a workaround, but strictly speaking, not a solution for what you've asked now. – TLama Jan 11 '14 at 11:11

1 Answers1

4

There seems to be no direct way to save image element to a file, so I would get it from cache. To copy a file from the IE cache by a given resource URL you may use a function like follows. The URL parameter is the src attribute value of the HTML tag and FileName is the target file name:

uses
  WinInet;

procedure SaveInetResourceToFile(const URL, FileName: string);
var
  BufferSize: DWORD;
  CacheEntry: PInternetCacheEntryInfo;
begin
  if not RetrieveUrlCacheEntryFile(PChar(URL), TInternetCacheEntryInfo(nil^),
    BufferSize, 0) then
  begin
    if GetLastError = ERROR_INSUFFICIENT_BUFFER then
    begin
      GetMem(CacheEntry, BufferSize);
      try
        if RetrieveUrlCacheEntryFile(PChar(URL), CacheEntry^, BufferSize, 0) then
        try
          Win32Check(CopyFile(CacheEntry.lpszLocalFileName, PChar(FileName),
            False));
        finally
          Win32Check(UnlockUrlCacheEntryFile(PChar(URL), 0));
        end
        else
          RaiseLastOSError;
      finally
        FreeMem(CacheEntry);
      end;
    end
    else
      RaiseLastOSError;
  end;
end;
TLama
  • 75,147
  • 17
  • 214
  • 392