在运行时创建和填充 ImageList
Create and populate an ImageList at runtime
从 C# 和 Visual Studio 到 Delphi 10.1 Berlin 对我来说很难,但一些性能是至关重要的,我已经很长时间没有使用 Delphi (超过10年),所以我被屏蔽了。
我需要在 运行 时创建一个 ImageList 并将其存储在单例对象中,但由于读取内存时出现异常,我无法这样做。
这是我的代码的摘录:
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;
这里有什么问题吗?
您没有 return 来自 GetBmpOf
的任何内容。您必须分配给 Result
变量。)
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;
您还泄漏了 PNG 图像 _man24
,无论如何它应该是一个局部变量。您在某些地方硬编码 24 的大小,但在其他地方则不然。你的 try except 块是没有意义的。
从 C# 和 Visual Studio 到 Delphi 10.1 Berlin 对我来说很难,但一些性能是至关重要的,我已经很长时间没有使用 Delphi (超过10年),所以我被屏蔽了。
我需要在 运行 时创建一个 ImageList 并将其存储在单例对象中,但由于读取内存时出现异常,我无法这样做。
这是我的代码的摘录:
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;
这里有什么问题吗?
您没有 return 来自 GetBmpOf
的任何内容。您必须分配给 Result
变量。)
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;
您还泄漏了 PNG 图像 _man24
,无论如何它应该是一个局部变量。您在某些地方硬编码 24 的大小,但在其他地方则不然。你的 try except 块是没有意义的。