【发布时间】:2014-02-20 13:21:54
【问题描述】:
我尝试将 base64 字符串转换为位图,但随后我得到一个黑色图像.. 这是我用来解码的脚本:
function Base64ToBitmap(const S: string): TBitmap;
var
SS: TStringStream;
V: string;
begin
V := Decode(S);
SS := TStringStream.Create(V);
try
Result := TBitmap.Create;
Result.LoadFromStream(SS);
finally
SS.Free;
end;
end;
这是解码脚本:
function Decode(const Input: AnsiString): string;
var
bytes: TBytes;
utf8: UTF8String;
begin
bytes := EncdDecd.DecodeBase64(Input);
SetLength(utf8, Length(bytes));
Move(Pointer(bytes)^, Pointer(utf8)^, Length(bytes));
Result := string(utf8);
end;
位图到base64
function BitmapToBase64(ABitmap: TBitmap): string;
var
SS: TStringStream;
V: string;
begin
SS := TStringStream.Create('');
try
ABitmap.SaveToStream(SS);
V := SS.DataString;
Result := Encode(V);
finally
SS.Free;
end;
end;
编码:
function Encode(const Input: string): AnsiString;
var
utf8: UTF8String;
begin
utf8 := UTF8String(Input);
Result := EncdDecd.EncodeBase64(PAnsiChar(utf8), Length(utf8));
end;
为什么我会黑屏? base64 字符串是截图。
【问题讨论】:
-
请提供包含编码和解码的SSCCE
-
@DavidHeffernan 完成 :)
-
SSCCE 将包括从位图到字符串到 base 64 的完整往返行程,然后再返回。在一个程序中。
-
哦,我对 image1 做了一个截图,然后我通过 BitmapToBase64(image1.Picture.bitmap) 通过套接字发送图像然后我将它转换为: Image1.Picture.Bitmap.Assign(base64tobitmap(recievedtext) );