我正在做以下步骤将TBitmap(Firemonkey)转换为字符串:
function BitmapToBase64(Bitmap: Tbitmap): string;
var
BS: TBitmapSurface;
AStream: TMemoryStream;
begin
BS := TBitmapSurface.Create;
BS.Assign(Bitmap);
BS.SetSize(300, 200);
AStream := TMemoryStream.Create;
try
TBitmapCodecManager.SaveToStream(AStream, BS, '.png');
Result := TNetEncoding.Base64.EncodeBytesToString(AStream, AStream.Size);
finally
AStream.Free;
BS.Free;
end;
end;
如何将字符串还原为TBitmap?我做了以下哪个不生成TBitmap:
procedure Base64ToBitmap(AString: String; Result : Tbitmap);
var
ms : TMemoryStream;
BS: TBitmapSurface;
bytes : TBytes;
begin
bytes := TNetEncoding.Base64.DecodeStringToBytes(AString);
ms := TMemoryStream.Create;
try
ms.WriteData(bytes, Length(bytes));
ms.Position := 0;
BS := TBitmapSurface.Create;
BS.SetSize(300, 200);
try
TBitmapCodecManager.LoadFromStream(ms, bs);
Result.Assign(bs);
finally
BS.Free;
end;
finally
ms.Free;
end;
end;
我需要更小的base64字符串,以便我可以将它传输到Datasnap服务器。正常的base64字符串给我内存不足,因为字符串的大小超过200000 - 1000000的长度。
答案 0 :(得分:4)
在BitmapToBase64()
中,您将TMemoryStream
本身传递给TNetEncoding.Base64.EncodeBytesToString()
,Memory
不接受流作为输入开始。您需要传递流的function BitmapToBase64(Bitmap: Tbitmap): string;
var
BS: TBitmapSurface;
AStream: TMemoryStream;
begin
BS := TBitmapSurface.Create;
BS.Assign(Bitmap);
BS.SetSize(300, 200);
AStream := TMemoryStream.Create;
try
TBitmapCodecManager.SaveToStream(AStream, BS, '.png');
Result := TNetEncoding.Base64.EncodeBytesToString(AStream.Memory, AStream.Size);
finally
AStream.Free;
BS.Free;
end;
end;
属性的值:
error