Solved

Downloading an Image

Posted on 2004-05-02
5
291 Views
Last Modified: 2010-04-05
I need to know how to download an image from the web (preferably using Winsock), also I need to be able to download a new image every time (not using cache).

After downloading, either saving to file or loading it into an image box is fine.

Thank you.
0
Comment
Question by:bolaseal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 6

Expert Comment

by:Amir Azhdari
ID: 10974965
0
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 10974990
Procedure GetNetFile(mURLPath,trgPath:pchar);
var
  nread         : cardinal;
  cnt           : integer;
  etxt          : array [0..1023] of byte;
  mhhtreq       : HINTERNET;
  mhtturl       : HINTERNET;
  my_exe        : file of byte;
  fpath         : string;
  fnm           : string;
begin
 try
  DeleteUrlCacheEntry(mURLPath);
  mhhtreq:=InternetOpen('URName',INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0);
  try
   mhtturl:=InternetOpenUrl(mhhtreq,mURLPath,nil,0,0,0);
   AssignFile(my_exe,trgPath);
   Rewrite(my_exe);
  repeat
   FillChar(etxt,1024,#0);
   InternetReadFile(mhtturl,@etxt,sizeof(etxt),nread);
   for cnt:=0 to nread-1 do
      Write(my_exe,etxt[cnt]);
  until nread=0;
   CloseFile(my_exe);
  finally
   InternetCloseHandle(mhtturl);
  end;
 finally
    InternetCloseHandle(mhhtreq);
 end;
end;
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 10975052
Hello

  Try this function

uses URLMon;

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


procedure TForm1.Button2Click(Sender: TObject);
begin
  if DownloadFile('http://www.google.com/images/logo.gif'
                  ,'c:\pic.gif') then
    ShowMessage('Done')
  else
    ShowMessage('Errod downloading the file');
end;

Regards,
Mohammed
0
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 390 total points
ID: 10975237
and here's another example using Indy idHTTP

procedure TForm1.Button1Click(Sender: TObject);
var
  FS : TFileStream;
  MS : TMemoryStream;
begin
  MS := TMemoryStream.Create;
  Idhttp1.Get('http://www.google.com/images/logo.gif',MS);
  FS := TFileStream.Create('C:\MyPic.gif',fmCreate      );
  Ms.Position := 0;
  FS.CopyFrom(MS,MS.Size);
  FS.Free;
  MS.Free
end;

Regards,
Mohammed
0
 

Author Comment

by:bolaseal
ID: 10983282
Thank you everyone, mnasman, I used your method since the idHTTP control also lets me set custom headers ^_^
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi : could not find program, '...exe' 2 215
Making delphi communicate with a c# service 16 124
Convert a string into a TDateTime 5 73
firemonkey keyboard covers the controls 1 53
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question