WriteBinaryStream compressed to INI file?

≯℡__Kan透↙ 提交于 2020-08-08 06:29:16

问题


In Delphi 10.4, I try to save a valid TPicture compressed to an INI file, trying to replicate the ZLibCompressDecompress example from the documentation:

procedure TForm1.SavePictureToIniFile(const APicture: TPicture);
// https://stackoverflow.com/questions/63216011/tinifile-writebinarystream-creates-exception
var
  LInput: TMemoryStream;
  LOutput: TMemoryStream;
  MyIni: System.IniFiles.TMemIniFile;
  ThisFile: string;
  LZip: TZCompressionStream;
begin
  if FileSaveDialog1.Execute then
    ThisFile := FileSaveDialog1.FileName
  else EXIT;

  LInput := TMemoryStream.Create;
  LOutput := TMemoryStream.Create;
  LZip := TZCompressionStream.Create(clDefault, LOutput);
  try
    APicture.SaveToStream(LInput);
    LInput.Position := 0;
    //LOutput.Position := 0;
    LZip.CopyFrom(LInput, LInput.Size);

    MyIni := TMemIniFile.Create(ThisFile);
    try
      MyIni.WriteBinaryStream('Custom', 'IMG', LOutput);
      MyIni.UpdateFile;
    finally
      MyIni.Free;
    end;
  finally
    LInput.Free;
    LOutput.Free;
    LZip.Free;
  end;
end;

But the stream is not saved in the INI file. The resulting INI file contains only these lines:

[Custom]
IMG=

So how can I save the compressed stream in the INI file?


回答1:


You need to set LOutput.Position := 0 after the LZip.CopyFrom line, that is, immediately before

MyIni.WriteBinaryStream('Custom', 'IMG', LOutput);


来源:https://stackoverflow.com/questions/63217195/writebinarystream-compressed-to-ini-file

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