png - 如何知道 TPNGObject 是否具有有效的 header ?

标签 png delphi-7 rtti

我有以下类(class):

    TPNGButton = class(TNeoGraphicControl)
    private
        FImageDown: TPNGObject;
        fImageNormal: TPNGObject;
        fImageOver: TPNGObject;
    ...

    public
    ...
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
    published
        property ImageNormal: TPNGObject read fImageNormal write SetImageNormal;
        property ImageDown: TPNGObject read FImageDown write SetImageDown;
        property ImageOver: TPNGObject read FImageOver write SetImageOver;
    ...
end;

我正在使用下面的函数来复制 objFrom 的属性至 objTo作为参数。
procedure CopyObject(ObjFrom, ObjTo: TObject);
var
  PropInfos: PPropList;
  PropInfo: PPropInfo;
  Count, Loop: Integer;
  OrdVal: Longint;
  StrVal: String;
  FloatVal: Extended;
  MethodVal: TMethod;
begin
  { Iterate thru all published fields and properties of source }
  { copying them to target }

  { Find out how many properties we'll be considering }
  Count := GetPropList(ObjFrom.ClassInfo, tkAny, nil);
  { Allocate memory to hold their RTTI data }
  GetMem(PropInfos, Count * SizeOf(PPropInfo));
  try
    { Get hold of the property list in our new buffer }
    GetPropList(ObjFrom.ClassInfo, tkAny, PropInfos);
    { Loop through all the selected properties }
    for Loop := 0 to Count - 1 do
    begin
      PropInfo := GetPropInfo(ObjTo.ClassType, PropInfos^[Loop]^.Name);
      { Check the general type of the property }
      { and read/write it in an appropriate way }
      case PropInfos^[Loop]^.PropType^.Kind of
        tkInteger, tkChar, tkEnumeration,
        tkSet, tkClass{$ifdef Win32}, tkWChar{$endif}:
        begin
          OrdVal := GetOrdProp(ObjFrom, PropInfos^[Loop]);
          if Assigned(PropInfo) and (Assigned(PropInfo^.SetProc)) then
            SetOrdProp(ObjTo, PropInfo, OrdVal); //here happens the bug...
        end;
        tkFloat:
        begin
          FloatVal := GetFloatProp(ObjFrom, PropInfos^[Loop]);
          if Assigned(PropInfo) and (Assigned(PropInfo^.SetProc)) then
            SetFloatProp(ObjTo, PropInfo, FloatVal);
        end;
        {$ifndef DelphiLessThan3}
        tkWString,
        {$endif}
        {$ifdef Win32}
        tkLString,
        {$endif}
        tkString:
        begin
          { Avoid copying 'Name' - components must have unique names }
          if UpperCase(PropInfos^[Loop]^.Name) = 'NAME' then
            Continue;
          StrVal := GetStrProp(ObjFrom, PropInfos^[Loop]);
          if Assigned(PropInfo) and (Assigned(PropInfo^.SetProc)) then
            SetStrProp(ObjTo, PropInfo, StrVal);
        end;
        tkMethod:
        begin
          MethodVal := GetMethodProp(ObjFrom, PropInfos^[Loop]);
          if Assigned(PropInfo) and (Assigned(PropInfo^.SetProc)) then
            SetMethodProp(ObjTo, PropInfo, MethodVal);
        end
      end
    end
  finally
    FreeMem(PropInfos, Count * SizeOf(PPropInfo));
  end;
end;

但是,当在 SetOrdProp 中传递 PNGObject 属性时,Delphi 返回以下异常:
Exception

我的问题是:
我怎么知道 PNGObject在 SetOrdProp 之前有一个有效的 header 吗?或者用另一种方法来解决这个问题......

其他评论
  • 使用 Assign方法为以下代码并注释CopyObject功能:
      TControl(objCtrlZ.Referencia).Assign(Component);
      // "Component" is objFrom and objCtrlZ.Referencia is objTo
      //  CopyObject(Component, objCtrlZ.Referencia);
    

  • Delphi 捕获以下异常:

    Exception 2

    最佳答案

    我解决了我的问题。

    验证是否 TPNGObject有一个有效的标题我使用了这个代码:

    objTemp := GetObjectProp(ObjFrom,PropInfos^[Loop]);
    if ((TPNGObject(objTemp).Chunks.Count <> 0) and (TPNGObject(objTemp).Chunks.Item[0] is TChunkIHDR)) then begin ... end;
    

    第一行我得到属性 TPNGObject和往常一样,对象被分配了 objTemp无法获得 AV。

    为了验证标题,我在 Chunks 中验证 count 是否不为零以及 Item[0] 是否为 TChunkIHDR 以了解是否为有效标题。

    谢谢!

    关于png - 如何知道 TPNGObject 是否具有有效的 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27848756/

    相关文章:

    png - alpha channel 和透明 channel 有什么区别?

    python - IOError : "decoder zip not available" using matplotlib PNG in ReportLab on Linux, 适用于 Windows

    Delphi 7 IDE 堆栈溢出错误

    delphi - Delphi 7 中停止事件传播

    delphi - 有什么方法可以获取 Delphi 7 中旧样式(Borland Pascal)对象实例的类名吗?

    java小程序是否可以将小程序制作成png文件

    delphi - 调用 ShowModal 时,表单隐藏在其他表单后面

    delphi - 如何从 TRttiMethod 获取/创建匿名方法?

    delphi - 是否可以使用 RTTI 获取接口(interface)上 GUID 的值?

    java - 9-patch 文件中出现奇怪的黑边 - android