【发布时间】:2020-11-22 17:27:44
【问题描述】:
在 Delphi 10.4 中,我尝试将有效的 TPicture base64 编码保存到 INI 文件中:
procedure TForm1.SavePictureToIniFile(const APicture: TPicture);
var
LInput: TMemoryStream;
LOutput: TMemoryStream;
MyIni: TIniFile;
ThisFile: string;
begin
if FileSaveDialog1.Execute then
ThisFile := FileSaveDialog1.FileName
else EXIT;
LInput := TMemoryStream.Create;
LOutput := TMemoryStream.Create;
try
APicture.SaveToStream(LInput);
LInput.Position := 0;
TNetEncoding.Base64.Encode(LInput, LOutput);
LOutput.Position := 0;
MyIni := TIniFile.Create(ThisFile);
try
MyIni.WriteBinaryStream('Custom', 'IMG', LOutput); // Exception# 234
finally
MyIni.Free;
end;
finally
LInput.Free;
LOutput.Free;
end;
end;
WriteBinaryStream 创建异常:
ERROR_MORE_DATA 234 (0xEA) 有更多数据可用。
为什么?这是什么意思?这个问题怎么解决?
编辑:考虑到@Uwe Raabe 和@Andreas Rejbrand 所说的,这段代码(不使用base64 编码)现在可以工作了:
procedure TForm1.SavePictureToIniFile(const APicture: TPicture);
var
LInput: TMemoryStream;
MyIni: System.IniFiles.TMemIniFile;
ThisFile: string;
begin
if FileSaveDialog1.Execute then
ThisFile := FileSaveDialog1.FileName
else EXIT;
LInput := TMemoryStream.Create;
try
APicture.SaveToStream(LInput);
LInput.Position := 0;
MyIni := TMemIniFile.Create(ThisFile);
try
MyIni.WriteBinaryStream('Custom', 'IMG', LInput);
MyIni.UpdateFile;
finally
MyIni.Free;
end;
finally
LInput.Free;
end;
end;
【问题讨论】:
-
您应该使用没有 Base64 编码的 WriteBinaryStream 或编码为 Base64 并改用 WriteString。 Base64 已经是一种文本表示。将其作为二进制流会使所需的内存加倍。
-
@UweRaabe:更不用说结果字符串甚至不是图像的 Base64 编码版本!
-
(请注意,当前的解决方案实际上并没有使用 Base64。)
标签: delphi stream ini delphi-10.4-sydney