Base64 到 Inno Setup Pascal 脚本中的位图?

Base64 to bitmap in Inno Setup Pascal Script?

我有一个 XML 带有一些 Base64 标志。

我想解码它们以便在我的安装程序列表框中显示它们,有什么办法可以做到吗?

要将 Base64 字符串转换为实际的二进制数据,您可以使用 CryptStringToBinary Windows API function.

function CryptStringToBinary(
  sz: string; cch: LongWord; flags: LongWord; binary: string; var size: LongWord;
  skip: LongWord; flagsused: LongWord): Integer;
  external 'CryptStringToBinaryW@crypt32.dll stdcall';

const
  CRYPT_STRING_BASE64 = ;

procedure LoadBitmapFromBase64(Bitmap: TBitmap; S: string);
var
  Stream: TStream;
  Buffer: string;
  Size: LongWord;
begin
  Stream := TStringStream.Create('');
  try
    SetLength(Buffer, (Length(S) div 2) + 1);
    Size := Length(S);
    if CryptStringToBinary(S, Length(S), CRYPT_STRING_BASE64, Buffer, Size, 0, 0) = 0 then
    begin
      RaiseException('Error decoding Base64 string');
    end;

    Stream.WriteBuffer(Buffer, Size);

    Stream.Position := 0;
    Bitmap.LoadFromStream(Stream);
  finally
    Stream.Free;
  end;
end;

代码需要 Inno Setup 的 Unicode 版本(Inno Setup 6 的唯一版本)。无论如何,在 21 世纪,您不应该使用 Ansi 版本。具有讽刺意味的是,在 Ansi 版本中实现它会更容易。请参阅 了解与 Ansi 和 Unicode 版本的 Inno Setup 兼容的 CryptStringToBinary 的使用。