Search code examples
androidimagedelphipngfiremonkey

Delphi Android download remote PNG to TImage


Trying to assign remote PNG file to TImage. I've tried following, it says "Invalid stream format". I have no clue how to download PNG image into Firemonkey styled apps (XE8):

procedure TfrmMain.Button1Click(Sender: TObject);
var qrString: String;
  MS : TMemoryStream;
  Png: TImageMultiResBitmap;
begin
  MS := TMemoryStream.Create;
  Png := TImageMultiResBitmap.Create(Png);
  try
    IdHTTP1.get('https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=chujisko',MS);
    Ms.Seek(0,soFromBeginning);
    Png.LoadFromStream(MS);
    img1.MultiResBitmap.Assign(Png);

  finally
    FreeAndNil(Png);
    FreeAndNil(MS);
  end;

Solution

  • So guys, this seems to be the correct and reliable solution:

    procedure TfrmMain.Button1Click(Sender: TObject);
    var qrString: String;
        qrDims: integer;
        MS : TMemoryStream;
    begin
    if Edit1.Text <> '' then begin
      qrDims := Screen.Width - 100;
      qrString := 'http://chart.googleapis.com/chart?chs=' + IntToStr(qrDims) + 'x' + IntToStr(qrDims) + '&cht=qr&chl=' + Edit1.Text;
      MS := TMemoryStream.Create;
      try
        IdHTTP1.get(qrString, MS);
        Ms.Seek(0,soFromBeginning);
        img1.Bitmap.LoadFromStream(MS);
        img1.Bitmap.SaveToFile();
      finally
        FreeAndNil(MS);
      end;
    end else begin
      ShowMessage('Please, input a text.');
    end;
    end;