Solved

Downloading an Image

Posted on 2004-05-02
5
287 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:mnasman
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:
mnasman 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

12 Experts available now in Live!

Get 1:1 Help Now