Delphi Android download remote PNG to TImage

房东的猫 提交于 2019-12-13 04:35:55

问题


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;

回答1:


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;


来源:https://stackoverflow.com/questions/32359746/delphi-android-download-remote-png-to-timage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!