Using TidHttp to load Jpeg images from a URL (only those that exist)?

I am trying to extract a large number of images from the Internet using the TidHttp component.

The problem is that the number of images is missing (Example: 7403, 7412, etc.)

How to check only those that exist and save them in a 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; 
+6
source share
1 answer

You do not need to do anything for this. If you try to access a nonexistent URL, the HTTP server will report an error that is TIdHTTP , which is what EIdHTTPProtocolException into an EIdHTTPProtocolException . You do not need to worry about calling TIdHTTP.Head() in the first place, since you load images into TMemoryStream before saving them. You can catch an exception when calling TIdHTTP.Get() yourself, you don't need to check the ResponseCode at all.

Try the following:

 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 really don't need TImage to save data to a file. If you can omit the TImage.Picture.Assign() step, then the code is a bit simpler by excluding TJPEGImage as a whole (if you are not trying to verify the download files are valid), for example:

 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; 
+11
source

Source: https://habr.com/ru/post/902805/


All Articles