Download picture

Hi!


Is possible to download a picture with Sparkle client?

I tried this code, but gives me "JPEG error 42"


procedure GetCusomImage(Url: string; Pic: TPicture);
var
  Client: THttpClient;
  Request: THttpRequest;
  Response: THttpResponse;
  ResponseBody: string;
  stream: TMemoryStream;
  lPNG: TPngImage;
  lJPEG: TJPEGImage;
begin
  try
    Client := THttpClient.Create;
    Log(llInfo, 'Gettings orders list from URL: '+Url);
    Request := Client.CreateRequest;
    Request.Uri := Url;
    Request.Method := 'GET';
    Request.SetContent(TEncoding.UTF8.GetBytes('Request content'));
    Response := Client.Send(Request);
    if Response.StatusCode = 200 then begin


      stream := TMemoryStream(Response.ContentAsStream);
      stream.Position := 0;


      if Url.EndsWith('.jpg') or Url.EndsWith('.jpeg') then begin
        lJPEG := TJPEGImage.Create;
        try
          lJPEG.LoadFromStream(stream);
          Pic.Assign(lJPEG);              <---------------- Error here
        finally
          lJPEG.Free;
        end;
      end;
    end else begin
      Log(llError, 'URL call failed: '+IntToStr(Response.StatusCode));
    end;
  finally
    Request.Free;
    Response.Free;
    Client.Free;
  end;
end;


What am I doing wrong?

Hi Dino, 

Try this:

It's not working: Here's the URI that results in an JPEH 42 error:


https://st.depositphotos.com/2044631/2014/i/380/depositphotos_20146623-stock-photo-tigers-face.jpg


Please try this then:




procedure GetCusomImage(Url: string; Pic: TPicture);
var
  Client: THttpClient;
  Request: THttpRequest;
  Response: THttpResponse;
  lJPEG: TJPEGImage;
  Bytes: TBytesStream;
begin
  Request := nil;
  Response := nil;
  Client := THttpClient.Create;
  try
    Request := Client.CreateRequest;
    Request.Uri := Url;
    Request.Method := 'GET';
    Request.SetContent(TEncoding.UTF8.GetBytes('Request content'));
    Response := Client.Send(Request);
    if Response.StatusCode = 200 then begin


      if Url.EndsWith('.jpg') or Url.EndsWith('.jpeg') then begin
        lJPEG := TJPEGImage.Create;
        Bytes := nil;
        try
          Bytes := TBytesStream.Create(Response.ContentAsBytes);
          lJPEG.LoadFromStream(Bytes);
          Pic.Assign(lJPEG);
        finally
          lJPEG.Free;
          Bytes.Free;
        end;
      end;
    end else begin
      ShowMessage('URL call failed: '+IntToStr(Response.StatusCode));
    end;
  finally
    Request.Free;
    Response.Free;
    Client.Free;
  end;
end;

it's working, thank you