Delphi Firemonkey通过RTTI获取/设置TBitmap
如何通过RTTI获取/设置TBitmap?我已做了以下工作:Delphi Firemonkey通过RTTI获取/设置TBitmap,delphi,firemonkey,tbitmap,Delphi,Firemonkey,Tbitmap,如何通过RTTI获取/设置TBitmap?我已做了以下工作: function TMyWebCam.BitmapToString: string; var RContext : TRttiContext; RType: TRttiType; prop: TRttiProperty; value : TValue; begin RContext := TRttiContext.Create; RType := RContext.GetType(ClassType); p
function TMyWebCam.BitmapToString: string;
var
RContext : TRttiContext;
RType: TRttiType;
prop: TRttiProperty;
value : TValue;
begin
RContext := TRttiContext.Create;
RType := RContext.GetType(ClassType);
prop := RType.GetProperty('screenshot');
prop.GetValue(Self).TryCast(TypeInfo(TBitmap), value);
Result := value.ToString;
end;
procedure TMyWebCam.SetScreenshot(AString: String);
var
RContext : TRttiContext;
RType: TRttiType;
prop: TRttiProperty;
value : TValue;
begin
RContext := TRttiContext.Create;
RType := RContext.GetType(ClassType);
prop := RType.GetProperty('screenshot');
value.Cast(TypeInfo(TBitmap));
value.From<String>(AString);
prop.SetValue(Self, value);
end;
这给了我:(TBitmapOfItem@07A06280)
要将字符串转换为TBitmap,请执行以下操作:
function TMyWebCam.BitmapToString: string;
var
RContext : TRttiContext;
RType: TRttiType;
prop: TRttiProperty;
value : TValue;
begin
RContext := TRttiContext.Create;
RType := RContext.GetType(ClassType);
prop := RType.GetProperty('screenshot');
prop.GetValue(Self).TryCast(TypeInfo(TBitmap), value);
Result := value.ToString;
end;
procedure TMyWebCam.SetScreenshot(AString: String);
var
RContext : TRttiContext;
RType: TRttiType;
prop: TRttiProperty;
value : TValue;
begin
RContext := TRttiContext.Create;
RType := RContext.GetType(ClassType);
prop := RType.GetProperty('screenshot');
value.Cast(TypeInfo(TBitmap));
value.From<String>(AString);
prop.SetValue(Self, value);
end;
我必须将TBitmap转换为字符串,以便将其作为广播消息传递给另一个客户端。即使您可以通过RTTI检索
TBitmap
(在已经有指向TBitmap
的直接指针的同一个类中这样做是无用的),为什么您认为让RTTI将其转换为字符串对于传输它来说是一个有意义的操作?事实并非如此。RTTI只是将对象转换为对象内存地址的字符串表示形式,这就是您所看到的。对于传输,您需要序列化图像数据。例如,将TBitmap
保存到TMemoryStream
并将生成的字节编码到base64,然后传输该字符串。在接收器上反转。编码到base64字符串大小非常大,这就是我尝试使用RTTI的原因。我正在尝试对客户端的广播频道进行数据捕捉TDSAdmin。发件人在发送到其他客户端时被挂起。好的,Remy,我如何将以下函数的结果还原回TBitmap:函数BitmapToBase64(位图:TBitmap):字符串;var BS:TBitmapSurface;AStream:TStringStream;开始BS:=TBitmapSurface.Create;分配(位图);BS.设置尺寸(300200);AStream:=TStringStream.Create;试试tbitmapcodemanager.SaveToStream(AStream,BS,'.png');结果:=TIdEncoderMIME.EncodeString(AStream.DataString)最终为AStream.Free;BS.免费;结束;结束;还有其他比base64更精简的二进制到字符串格式,例如yEnc。或者考虑使用压缩。关键是,你试图用RTTI做的事情与你试图用广播做的事情是不可能或不合适的。您需要序列化。您不应该将位图保存到TStringStream
,它不是字符串数据。改为使用TMemoryStream
,然后使用tidecoderMime.EncodeStream()
。另一方面,使用tidecodermime.DecodeStream()
将其解码为TMemoryStream
,然后将其加载到位图中。我不熟悉DataSnap,您真的必须以字符串形式广播数据吗?您不能广播二进制数据吗?即使您可以通过RTTI检索TBitmap
(在已经有直接指针指向TBitmap
)的同一类中这样做是无用的),为什么您认为让RTTI将其转换为字符串将是传输它的一个有意义的操作?事实并非如此。RTTI只是将对象转换为对象内存地址的字符串表示形式,这就是您所看到的。对于传输,您需要序列化图像数据。例如,将TBitmap
保存到TMemoryStream
并将生成的字节编码到base64,然后传输该字符串。在接收器上反转。编码到base64字符串大小非常大,这就是我尝试使用RTTI的原因。我正在尝试对客户端的广播频道进行数据捕捉TDSAdmin。发件人在发送到其他客户端时被挂起。好的,Remy,我如何将以下函数的结果还原回TBitmap:函数BitmapToBase64(位图:TBitmap):字符串;var BS:TBitmapSurface;AStream:TStringStream;开始BS:=TBitmapSurface.Create;分配(位图);BS.设置尺寸(300200);AStream:=TStringStream.Create;试试tbitmapcodemanager.SaveToStream(AStream,BS,'.png');结果:=TIdEncoderMIME.EncodeString(AStream.DataString)最终为AStream.Free;BS.免费;结束;结束;还有其他比base64更精简的二进制到字符串格式,例如yEnc。或者考虑使用压缩。关键是,你试图用RTTI做的事情与你试图用广播做的事情是不可能或不合适的。您需要序列化。您不应该将位图保存到TStringStream
,它不是字符串数据。改为使用TMemoryStream
,然后使用tidecoderMime.EncodeStream()
。另一方面,使用tidecodermime.DecodeStream()
将其解码为TMemoryStream
,然后将其加载到位图中。我不熟悉DataSnap,您真的必须以字符串形式广播数据吗?你不能广播二进制数据吗?