unit Base64;interface uses EncdDecd,Graphics,Classes; function BitmapToString(Bitmap:TBitmap):String; function StringToBitmap(str:String):TBitmap; implementation function BitmapToString(Bitmap:TBitmap):String; var BitmapStream:TMemoryStream; strStream:TStringStream; begin BitmapStream:=TMemoryStream.Create; strStream:=TStringStream.Create(''); Bitmap.SaveToStream(BitmapStream); BitmapStream.Position:=0; EncodeStream(BitmapStream,strStream); Result:=strStream.DataString; BitmapStream.Free; strStream.Free; end;function StringToBitmap(str:String):TBitmap; var BitmapStream:TMemoryStream; strStream:TStringStream; begin strStream:=TStringStream.Create(str); BitmapStream:=TMemoryStream.Create; DecodeStream(strStream,BitmapStream); BitmapStream.Position:=0; result:=TBitmap.Create; result.LoadFromStream(BitmapStream); BitmapStream.Free; strStream.Free; end; end.
1、通过IStream传递(详情可参考MSDN或者Delphi帮助)
2、将Stream包装到VarArray中。可以查看VarArrayLock函数的帮助。
3、用Base64编码将Stream内容编码成字符串,然后作为字符串发送。后两种方法都需要接收方对数据进行逆向解码还原。
用Base64编码将Stream内容编码成字符串,然后作为字符串发送
Delphi的EncdDecd单元,其中有这样几个函数
procedure EncodeStream(Input, Output: TStream);
procedure DecodeStream(Input, Output: TStream);
function EncodeString(const Input: string): string;
function DecodeString(const Input: string): string;TBitmap->TStream->string 发送 ->TStream->TBitmap
Stream 和字符串之间的转换用EncodeStream和DecodeStream就可以了.
uses EncdDecd,Graphics,Classes;
function BitmapToString(Bitmap:TBitmap):String;
function StringToBitmap(str:String):TBitmap;
implementation
function BitmapToString(Bitmap:TBitmap):String;
var
BitmapStream:TMemoryStream;
strStream:TStringStream;
begin
BitmapStream:=TMemoryStream.Create;
strStream:=TStringStream.Create('');
Bitmap.SaveToStream(BitmapStream);
BitmapStream.Position:=0;
EncodeStream(BitmapStream,strStream);
Result:=strStream.DataString;
BitmapStream.Free;
strStream.Free;
end;function StringToBitmap(str:String):TBitmap;
var
BitmapStream:TMemoryStream;
strStream:TStringStream;
begin
strStream:=TStringStream.Create(str);
BitmapStream:=TMemoryStream.Create;
DecodeStream(strStream,BitmapStream);
BitmapStream.Position:=0;
result:=TBitmap.Create;
result.LoadFromStream(BitmapStream);
BitmapStream.Free;
strStream.Free;
end;
end.