Delphi Firemonkey通过RTTI获取/设置TBitmap

时间:2016-05-29 15:16:34

标签: 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);
  prop := RType.GetProperty('screenshot');
  prop.GetValue(Self).TryCast(TypeInfo(TBitmap), value);
  Result := value.ToString;
end;

给了我:(TBitmapOfItem @ 07A06280) 要将字符串转换为TBitmap,请执行以下操作:

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;

这是我的班级

  TMyWebCam = class
  private
    fscreenshot: TBitmap;
  public
    constructor Create;
    destructor Destroy;override;
    property screenshot: TBitmap read fscreenshot write fscreenshot;
    function BitmapToString : string;
    procedure SetScreenshot(AString : String);
  end;

constructor TMyWebCam.Create;
begin
  fscreenshot := TBitmap.Create;
end;

destructor TMyWebCam.Destroy;
begin
  fscreenshot.Free;
  inherited;
end;

我必须将TBitmap转换为字符串,以便将其作为广播消息传递给另一个客户端。

0 个答案:

没有答案