Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 在运行时创建并填充ImageList_Delphi_Runtime_Imagelist - Fatal编程技术网

Delphi 在运行时创建并填充ImageList

Delphi 在运行时创建并填充ImageList,delphi,runtime,imagelist,Delphi,Runtime,Imagelist,从C#和Visual Studio到Delphi 10.1 Berlin对我来说非常困难,但是一些性能是至关重要的,而且我已经很长时间(超过10年)没有使用Delphi了,所以我被阻止了 我需要在运行时创建一个ImageList并将其存储在一个singleton对象中,但由于在读取内存时出现异常,我无法这样做 以下是我的代码摘录: ImagesRessource = class private _owner: TComponent; _imageList: TimageList;

从C#和Visual Studio到Delphi 10.1 Berlin对我来说非常困难,但是一些性能是至关重要的,而且我已经很长时间(超过10年)没有使用Delphi了,所以我被阻止了

我需要在运行时创建一个ImageList并将其存储在一个singleton对象中,但由于在读取内存时出现异常,我无法这样做

以下是我的代码摘录:

ImagesRessource = class
private
  _owner: TComponent;   
  _imageList: TimageList;
  _man24: TPngImage;
  constructor Create;
  function GetBmpOf(png: TPngImage): TBitmap;
public
  procedure  Initialize(own: TComponent);   
end;

implementation

constructor ImagesRessource.Create;
begin
  ;
end;

procedure ImagesRessource.Initialize(owner: TComponent);
var
  bmp: TBitmap;
  RS  : TResourceStream;
begin
  try
    _man24 := TPngImage.Create;
    RS := TResourceStream.Create(hInstance, 'man_24', RT_RCDATA);
    _man24.LoadFromStream(RS);
    bmp := GetBmpOf(_man24);
    _imageList := TimageList.Create(owner);
    _imageList.Width := 24;
    _imageList.Height := 24;
    _imageList.AddMasked(Bmp, Bmp.TransparentColor); // exception read memory here
  except
    raise;
  end;
end;

function ImagesRessource.GetBmpOf(png: TPngImage): TBitmap;
var
  bmp: TBitmap;
begin
  bmp := TBitmap.Create;
  bmp.Width := png.Width;
  bmp.Height := png.Height;
  png.Draw(bmp.Canvas, bmp.Canvas.ClipRect);
end;

这里怎么了?

您没有从
GetBmpOf
返回任何内容。必须为
结果
变量赋值。)


您还泄漏了PNG图像
\u man24
,它在任何情况下都应该是局部变量。在某些地方,您可以硬编码24的大小,但在其他地方则不行。你的试块是没有意义的

为什么需要创建ImageList、从ResourceStream读取图像并存储到ImageList中?也许您应该将ImageList放入表单或数据模块中,并在设计时添加映像。使用资源是@Kohull的明智之举。它允许您在修订控制下将资产保存在单独的文件中。一旦将其放入dfm文件中,维护起来就变得更加困难。
function ImagesRessource.GetBmpOf(png: TPngImage): TBitmap;
begin
  Result := TBitmap.Create;
  Result.Width := png.Width;
  Result.Height := png.Height;
  png.Draw(Result.Canvas, Result.Canvas.ClipRect);
end;