Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6889213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:07:40+00:00 2026-05-27T06:07:40+00:00

I am trying to retrieve a large number of images from the web using

  • 0

I am trying to retrieve a large number of images from the web using a TidHttp component.

The problem is that there is a number of images that are missing (Example: 7403, 7412, etc)

How do i test for only those that exist and save those to file?

procedure TForm.Button1Click(Sender: TObject);
var
  MS : TMemoryStream;
  JPEGImage: TJPEGImage;
  Url, numString: String;
  I, Code: Integer;
begin
 for I := 7400 to 7500 do
 begin
 { 
   Url        :='http://www.mywebpage.com/images/DSC' + numString+  '.jpg';
   try
   idhttp1.Head(URL);
   code := idhttp1.ResponseCode;
   except on E: EIdHTTPProtocolException do
    code := idhttp1.ResponseCode;
   end;//try except
   if code = 200 then
   begin

   MS := TMemoryStream.Create;
   JPEGImage  := TJPEGImage.Create;
   try
     try
     idhttp1.Get(Url, MS); //Send the request and get the image
     code := idhttp1.ResponseCode;
     MS.Seek(0,soFromBeginning);
     JPEGImage.LoadFromStream(MS);//load the image in a Stream
     Image1.Picture.Assign(JPEGImage);//Load the image in a Timage component
     Image1.Picture.SaveToFile('C:\Museum_Data\DSC' + numString + '.jpg');
     Application.ProcessMessages;
     except
      on E: EIdHTTPProtocolException do
        code := idhttp1.ResponseCode; // or: code := E.ErrorCode;
     end; //try except
          finally
    MS.free;
    JPEGImage.Free;

  end; //try finally
  end; //if

end;
 end;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:07:41+00:00Added an answer on May 27, 2026 at 6:07 am

    You don’t have to do anything extra for that. If you try to access a non-existant URL, the HTTP server will report an error that TIdHTTP than wraps into an EIdHTTPProtocolException exception. You do not have to bother with calling TIdHTTP.Head() first, since you are downloading the images to a TMemoryStream before saving them. You can catch the exception when calling TIdHTTP.Get() by itself, no need to check the ResponseCode at all.

    Try this:

    procedure TForm.Button1Click(Sender: TObject); 
    var 
      MS: TMemoryStream; 
      JPEG: TJPEGImage; 
      Url: String; 
      I: Integer; 
    begin 
      MS := TMemoryStream.Create;
      try
        JPEG := TJPEGImage.Create; 
        try 
          for I := 7400 to 7500 do 
          begin 
            Url := 'http://www.mywebpage.com/images/DSC' + IntToStr(I) +  '.jpg';
            MS.Clear;
            try
              IdHTTP1.Get(Url, MS);
            except
              on E: EIdHTTPProtocolException do 
                Continue;
            end;
            MS.Position := 0; 
            JPEG.LoadFromStream(MS);
            Image1.Picture.Assign(JPEG);
            JPEG.SaveToFile('C:\Museum_Data\DSC' + IntToStr(I) + '.jpg'); 
            Application.ProcessMessages; 
          end;
        finally
          JPEG.Free;
        end;
      finally 
        MS.Free;
      end;
    end;
    

    You do not actually need the TImage in order to save the data to file. If you can omit the TImage.Picture.Assign() stage, then the code a bit simpler by eliminating the TJPEGImage altogether (unless you are trying to validate the download files are valid), eg:

    procedure TForm.Button1Click(Sender: TObject); 
    var 
      MS: TMemoryStream; 
      Url: String; 
      I: Integer; 
    begin 
      MS := TMemoryStream.Create;
      try
        for I := 7400 to 7500 do 
        begin 
          Url := 'http://www.mywebpage.com/images/DSC' + IntToStr(I) +  '.jpg';
          MS.Clear;
          try
            IdHTTP1.Get(Url, MS);
          except
            on E: EIdHTTPProtocolException do 
              Continue;
          end;
          MS.Position := 0; 
          MS.SaveToFile('C:\Museum_Data\DSC' + IntToStr(I) + '.jpg'); 
          Application.ProcessMessages; 
        end;
      finally 
        MS.Free;
      end;
    end;
    

    Or:

    procedure TForm.Button1Click(Sender: TObject); 
    var 
      FS: TFileStream; 
      Url, FileName: String; 
      I: Integer; 
    begin 
      for I := 7400 to 7500 do 
      begin 
        Url := 'http://www.mywebpage.com/images/DSC' + IntToStr(I) +  '.jpg';
        FileName := 'C:\Museum_Data\DSC' + IntToStr(I) + '.jpg'; 
        FS := TFileStream.Create(FileName, fmCreate);
        try
          try
            try
              IdHTTP1.Get(Url, FS);
            except
              on E: EIdHTTPProtocolException do 
                Continue;
            end;
            Application.ProcessMessages; 
          finally
            Fs.Free;
          end;
        except
          DeleteFile(FileName);
        end;
      end;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to retrieve product information from magento by calling its web service using
I am trying to retrieve a list of images and text from a web
Using PyClips, I'm trying to build rules in Clips that dynamically retrieve data from
I am trying to retrieve a column from another related table, and display that
I'm trying to retrieve list of topics from a large corpus of news articles,
I am trying to retrieve a blob from a postgres database using the jdbc
I am trying to delete a large number of rows from MOTHER thanks to
I'm trying to have a single large UIView (for example a web view) which
I am trying retrieve user name from the code through graph api, the below
Iam trying to retrieve data from mysql database into stylesheet.php but it is not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.