先引用delphi自帶的單元
編碼
uses EncdDecd;code
///將Bitmap位圖轉化爲base64字符串
內存
function BitmapToString(img:TBitmap):string ;
字符串
var
string
ms:TMemoryStream;
it
ss:TStringStream;
io
s:string;
base64
begin
function
ms := TMemoryStream.Create;
map
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;
複製代碼