Solved

Downloading an Image

Posted on 2004-05-02
5
288 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now