The error is coming from pngImage.LoadFromStream(), which means TIdHTTP.Get() did its job. So this is not really an issue with Indy itself, but rather with the HTTP response. The SSL/TLS portion worked fine, or else you would have gotten an error from TIdHTTP.Get() instead.
Obviously, the HTTP server is not delivering PNG data like you are expecting, but you are not doing any kind of validation before attempting to load the stream into TPortableNetworkGraphic. What is the actual value of the TIdHTTP.Response.ContentType property after TIdHTTP.Get() is done?
What is the actual value of edtCodbarra.Text that you are testing with? So that we can see for ourselves what the server is actually returning.
Also, you are leaking all of your objects you Create(), you need to Free() them when you are done using them.
Try something more like this:
procedure TForm1.CarregaImagem2(url: String; Picture: TPicture);
var
iHttpImg : TIdHTTP;
imgStream : TMemoryStream;
pngImage : TPortableNetworkGraphic;
begin
ShowMessage(url);
pngImage := TPortableNetworkGraphic.Create;
try
imgStream := TMemoryStream.Create;
try
iHttpImg := TIdHTTP.Create;
try
iHttpImg.Request.UserAgent := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0; Waterfox) Gecko/20100101 Firefox/56.2.3';
iHttpImg.Request.Accept := 'image/png';
iHttpImg.Get(url, imgStream);
if not IsHeaderMediaType(iHttpImg.Response.ContentType, 'image/png') then
raise Exception.Create('Incorrect image type');
finally
iHttpImg.Free;
end;
imgStream.Position := 0;
pngImage.LoadFromStream(imgStream);
finally
imgStream.Free;
end;
Picture.Assign(pngImage);
finally
pngImage.Free;
end;
end;
Or maybe something like this:
procedure TForm1.CarregaImagem2(url: String; Picture: TPicture);
var
iHttpImg : TIdHTTP;
imgStream : TMemoryStream;
fileExt: string;
begin
ShowMessage(url);
imgStream := TMemoryStream.Create;
try
iHttpImg := TIdHTTP.Create;
try
iHttpImg.Request.UserAgent := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0; Waterfox) Gecko/20100101 Firefox/56.2.3';
iHttpImg.Request.Accept := 'image/*';
iHttpImg.Get(url, imgStream);
if not IsHeaderMediaType(iHttpImg.Response.ContentType, 'image') then
raise Exception.Create('Incorrect image type');
fileExt := ExtractFileExt(ExtractHeaderSubItem(iHttpImg.Response.ContentDisposition, 'filename', QuoteHTTP));
if fileExt = '' then
fileExt := ExtractFileExt(ExtractHeaderSubItem(iHttpImg.Response.ContentType, 'name', QuoteHTTP));
if fileExt = '' then
fileExt := GetMIMEDefaultFileExt(ExtractHeaderItem(iHttpImg.Response.ContentType));
if fileExt = '' then
raise Exception.Create('Unknown file extension');
finally
iHttpImg.Free;
end;
imgStream.Position := 0;
Picture.LoadFromStreamWithFileExt(imgStream, fileExt);
finally
imgStream.Free;
end;
end;