我正在尝试为TPicture编写一个marshaller,以便我可以通过datasnap方法发送包含TPicture的对象类。 我在寻找合适的方法时遇到了困难,我试图将TMemoryStream作为TObject发送,但似乎“图片中的数据丢失了”。我试图把图片放到一个字符串中 - 但是,我想这会给我带来各种麻烦,将二进制数据放入字符串中。 有没有人对如何正确编组TPicture有任何想法?
这就是我正在做的事情,到目前为止看起来我把图片放到了结果中,但是因为我创建了一个PictureMarshal类的实例,所以我得到了泄漏。 不要介意FChildren,它运作正常。
type
TPictureMarshall = class
FPictureName : string;
FPictureSize : Integer;
FPictureData : TBytes;
end;
TPersonMar = class(TObject)
private
FStringList : TStringList; // complex object that needs marshalling
FChildren: array of TPerson; // complex object that needs marshalling
FMar : TJsonMarshal;
FPicture : TPicture;
procedure RegisterConverters;
procedure SetStringlist(const Value: TStringList);
public
function Marshal : string; // should handle marshalling
property Stringlist : TStringList read FStringlist write SetStringlist;
constructor Create;
destructor Destroy; override;
procedure AddChild(AKid: TPerson);
end;
procedure TPersonMar.RegisterConverters;
begin
FMar.RegisterConverter(ClassType, 'FPicture', function(Data: TObject; Field: String): TObject
var
AMemoryStream: TMemoryStream;
APic : TPictureMarshall;
begin
AMemoryStream := TMemoryStream.Create;
try
result := TPictureMarshall.Create; // but who frees this?
FPicture.Graphic.SaveToStream(AMemoryStream);
TPictureMarshall(Result).FPictureName := 'Somepicturefilename.png';
TPictureMarshall(Result).FPictureSize := AMemoryStream.Size;
SetLength(TPictureMarshall(Result).FPictureData, TPictureMarshall(Result).FPictureSize);
ZeroMemory(TPictureMarshall(Result).FPictureData, TPictureMarshall(Result).FPictureSize);
AMemoryStream.Position := 0; // reset position
AMemoryStream.ReadBuffer(TPictureMarshall(Result).FPictureData, AMemoryStream.Size);
finally
AMemoryStream.Free;
end;
end);
end;
"FMar":null,"FPicture":{"type":"TestObjU.TPictureMarshall","id":14,"fields":{"FPictureName":"Somepicturefilename.png","FPictureSize":71290,"FPictureData":[137,80,78,71,13,...snip]}}}}