Solved

Downloading an Image

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

813 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

18 Experts available now in Live!

Get 1:1 Help Now