问题
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