how to convert idhttp downloaded image from extention to another?
<p>i have this Thread that get image url from the web then save it to memory stream then save from memory stream To file </p>

<p>i needed to convert any image that downloaded to a gif image so i do something like this </p>

<pre><code>unit downloadimgThread;

interface
uses Windows, SysUtils, Classes, dialogs, IdSSLOpenSSL, IdHttp, IdUri, System.AnsiStrings, Graphics, Jpeg, Vcl.Imaging.GIFImg, PNGImage;


type
  TDownloadUpdateVisualEvent = procedure(Sender: TObject; Anameofimg: String;
    var Aimagelocate: String) of object;

type
  TURLDownload = class(TThread)
  private
    FOnUpdateVisual: TDownloadUpdateVisualEvent;
    FURL: String;
    Fnameofimg: string;
    FPathImage: string;
    FFileNameImage: string;
    ImageName: string;
    PathURL: string;
    procedure DoUpdateVisual;
  protected
    procedure Execute; override;
  public
    constructor Create(Thrdid: Pointer; const AUrl: String;
      Const AOutPathImages: string; AOnUpdateVisual: TDownloadUpdateVisualEvent;
      Anameofimg: String); reintroduce;
    property URL: string read FURL write FURL;
    property PathImage: string read FPathImage;
    property FileNameImage: string read FFileNameImage;

  end;



var
URLDOWNLOAD: TURLDownload;

implementation

{ TURLDownload }



function JpgToGif(ms: TMemoryStream): Boolean;
var
  gif: TGIFImage;
  jpg: TJPEGImage;
begin
  Result := False;

  gif := TGIFImage.Create;
  try
    jpg := TJPEGImage.Create;
    try
      //jpg
      ms.Position := 0;
      jpg.LoadFromStream(ms);
      jpg.DIBNeeded;
      gif.Assign(jpg);

      //save...
      ms.Clear;
      gif.SaveToStream(ms);
      Result := True;
    finally
      jpg.Free;
      jpg := nil;
    end;
  finally
    gif.Free;
    gif := nil;
  end;
end;

constructor TURLDownload.Create(Thrdid: Pointer; const AUrl, AOutPathImages: string; AOnUpdateVisual: TDownloadUpdateVisualEvent; Anameofimg: String);
var
URI: TIdURI;
begin
inherited Create(false);
FreeOnTerminate := True;
FURL := AUrl;
FOnUpdateVisual := AOnUpdateVisual;
Fnameofimg := Anameofimg;
FPathImage := AOutPathImages;

URI := TIdURI.Create(AUrl);
try
ImageName := URI.Document;
PathURL := URI.path;
finally
URI.Free;
end;

end;

procedure TURLDownload.DoUpdateVisual;
begin
if Assigned(FOnUpdateVisual) then
FOnUpdateVisual(self, Fnameofimg, FFileNameImage);
end;

procedure TURLDownload.Execute;
var
aMs: TMemoryStream;
aIdHttp: TIdHttp;
IdSSL: TIdSSLIOHandlerSocketOpenSSL;
path: string;
dir: string;
SPEXT : String;
itsimage: string;
responsechk: Integer;
begin
dir := AnsiReplaceText(PathURL, '/', '');

if (ImageName = '') then
begin
exit;
end;

SPEXT := ExtractFileExt(ImageName);
ImageName := Copy(ImageName, 1, Length(ImageName) - Length(SPEXT));

path := PathImage + '\' + ImageName + '.gif';

if fileexists(path) then
begin
FFileNameImage := path;
if Assigned(FOnUpdateVisual) then
begin
Synchronize(DoUpdateVisual);
end;
exit;
end
else

if not fileexists(path) then
begin
aMs := TMemoryStream.Create;
aIdHttp := TIdHttp.Create(nil);
IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
try

IdSSL.SSLOptions.Method := sslvTLSv1;
IdSSL.SSLOptions.Mode := sslmUnassigned;
aIdHttp.HTTPOptions := [hoForceEncodeParams] + [hoNoProtocolErrorException];
aIdHttp.IOHandler := IdSSL;
aIdHttp.AllowCookies := True;
aIdHttp.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0';
aIdHttp.HandleRedirects := True;
aIdHttp.RedirectMaximum := 3;
try
aIdHttp.Head(trim(FURL));
except
end;
itsimage := aIdHttp.Response.ContentType;
responsechk := aIdHttp.ResponseCode;

if responsechk &lt;&gt; 200 then
begin
FFileNameImage := 'error';

if Assigned(FOnUpdateVisual) then
begin
Synchronize(DoUpdateVisual);
end;
exit;
end;


if (itsimage = 'image/gif') then
begin
try
aIdHttp.Get(trim(FURL), aMs);
except
end;
aMs.SaveToFile(path);
end else if (itsimage = 'image/jpeg') then
begin
try
aIdHttp.Get(trim(FURL), aMs);
except
end;
if JpgToGif(aMs) then
begin
aMs.SaveToFile(path);
end;
end;


try
if aIdHttp.Connected then
aIdHttp.Disconnect;

except

end;


finally
aMs.Free;
IdSSL.Free;
aIdHttp.Free;
end;
end;

FFileNameImage := path;

if Assigned(FOnUpdateVisual) then
begin
Synchronize(DoUpdateVisual);
end;

end;

end.
</code></pre>

<p>in this unit i try to check if image type is jpg then convert it to gif and save it specifically at this line of code </p>

<pre><code>if (itsimage = 'image/jpeg') then
begin
try
aIdHttp.Get(trim(FURL), aMs);
except
end;
if JpgToGif(aMs) then
begin
aMs.SaveToFile(path);
end;
// function to convert 
function JpgToGif(ms: TMemoryStream): Boolean;
var
  gif: TGIFImage;
  jpg: TJPEGImage;
begin
  Result := False;

  gif := TGIFImage.Create;
  try
    jpg := TJPEGImage.Create;
    try
      //jpg
      ms.Position := 0;
      jpg.LoadFromStream(ms);
      jpg.DIBNeeded;
      gif.Assign(jpg);

      //save...
      ms.Clear;
      gif.SaveToStream(ms);
      Result := True;
    finally
      jpg.Free;
      jpg := nil;
    end;
  finally
    gif.Free;
    gif := nil;
  end;
end;
</code></pre>

<p>when i try to convert the image and save it the image saved is corrupted what could be the issue ? </p>