Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Downloading an Image

Posted on 2004-05-02
5
Medium Priority
?
296 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 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 1560 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

664 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