如何将Bitmap位图与base64字符串相互转换[delphi]

来源:互联网 发布:阿里云安装php环境 编辑:程序博客网 时间:2024/05/07 17:12
uses EncdDecd;

然后就可以使用下面二个函数了:

by 菩提树下的杨过 http://yjmyzz.cnblogs.com/
///将Bitmap位图转化为base64字符串
function BitmapToString(img:TBitmap):string ;
var
  ms:TMemoryStream;
  ss:TStringStream;
  s:
string;
begin
    ms :
= TMemoryStream.Create;
    img.SaveToStream(ms);
    ss :
= TStringStream.Create('');
    ms.Position:
=0;
    EncodeStream(ms,ss);
//将内存流编码为base64字符流
    s:
=ss.DataString;
    ms.Free;
    ss.Free;
    result:
=s; 
end;

///将base64字符串转化为Bitmap位图
function StringToBitmap(imgStr:string):TBitmap;
var ss:TStringStream;
    ms:TMemoryStream;
    bitmap:TBitmap;
begin
    ss :
= TStringStream.Create(imgStr);
    ms :
= TMemoryStream.Create;
    DecodeStream(ss,ms);
//将base64字符流还原为内存流
    ms.Position:
=0;
    bitmap :
= TBitmap.Create;
    bitmap.LoadFromStream(ms);
    ss.Free;
    ms.Free;
    result :
=bitmap;

end;

参见:

http://www.pc100.net/delphi/zhuanti/delphi-pchar-string.html

http://www.pc100.net/delphi/zhuanti/delphi-override-overload.html

0 0