1

如何通过 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 转换为字符串,以便将其作为广播消息传递给另一个客户端。

4

0 回答 0