首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过TBitmapSurface返回到TBitmap的Delphi TBitmap

通过TBitmapSurface返回到TBitmap的Delphi TBitmap
EN

Stack Overflow用户
提问于 2016-06-03 05:37:20
回答 1查看 2.1K关注 0票数 2

我正在执行以下操作,将TBitmap(火猴)转换为字符串:

代码语言:javascript
复制
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的代码:

代码语言:javascript
复制
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服务器。当字符串的长度超过200000 - 1000000时,正常的base64字符串会耗尽内存。

EN

回答 1

Stack Overflow用户

发布于 2016-06-03 06:09:27

BitmapToBase64()中,您将TMemoryStream本身传递给TNetEncoding.Base64.EncodeBytesToString(),它一开始就不接受流作为输入。您需要传递流的Memory属性的值:

代码语言:javascript
复制
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;
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37602538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档