cxImageList运行时加载png图片和编辑

cxImageList运行时加载png图片和编辑

uses IOUtils, dxGDIPlusClasses, cxImageListEditor;

procedure TFormMain.LoadPngs(AImageList: TcxImageList; const AModule: string;
  ASourceSize: Integer = 128; ADestSize: Integer = 64);
var
  PicsPath, Path: String;
  Png: TdxPNGImage;
  Bmp, destBmp: TcxAlphaBitmap;
begin
  AImageList.SetSize(ADestSize, ADestSize);
  PicsPath := ExtractFilePath(ExtractFilePath(ParamStr(0))) + 'picture' +
    PathDelim + AModule;
  for Path in TDirectory.GetFiles(PicsPath, '*.png') do
  begin
    if FileExists(Path) then
    begin
      Png := TdxPNGImage.Create;
      Bmp := TcxAlphaBitmap.Create;
      destBmp := TcxAlphaBitmap.Create;
      try
        Png.SetSize(ASourceSize, ASourceSize);
        Bmp.SetSize(ASourceSize, ASourceSize);
        Png.LoadFromFile(Path);
        Bmp.Assign(Png);
        destBmp := TcxAlphaBitmap.CreateSize(ADestSize, ADestSize);
        cxSmoothResizeBitmap(Bmp, destBmp, true);
        AImageList.Add(destBmp, nil);
      finally
        Png.Free;
        Bmp.Free;
        destBmp.Free;
      end;
    end
    else
      ShowMessage('No Image:' + Path);
  end;
end;

 

procedure TForm1.Button2Click(Sender: TObject);
var
  AEditor: TcxImageListEditor;
begin
  AEditor := TcxImageListEditor.Create;
  try
    AEditor.Edit(cxImageList1);
  finally
    FreeAndNil(AEditor);
  end;
end;

 

procedure TFormMain.LoadIcos(AImageList: TcxImageList; const AModule: string;
  ASize: Integer = 64);
var
  LPicsPath, LPath: String;
  LIcon: TIcon;
begin
  if AImageList = nil then
    Exit;
  AImageList.SetSize(ASize, ASize);
  try
    LPicsPath := ExtractFilePath(ExtractFilePath(ParamStr(0))) + 'picture' +
      PathDelim + AModule;
    for LPath in TDirectory.GetFiles(LPicsPath, '*.ico') do
    begin
      if FileExists(LPath) then
      begin
        LIcon := TIcon.Create;
        try
          LIcon.SetSize(ASize, ASize);
          LIcon.LoadFromFile(LPath);
          AImageList.AddIcon(LIcon);
        finally
          LIcon.Free;
        end;
      end
      else
        ShowMessage('No image:' + LPath);
    end;
  except

  end;
end;
procedure TFormMain.LoadBmps(AImageList: TcxImageList; const AModule: string;
  ASourceSize: Integer = 128; ADestSize: Integer = 64);
var
  PicsPath, Path: String;
  Bmp, destBmp: TcxAlphaBitmap;
begin
  AImageList.SetSize(ADestSize, ADestSize);
  PicsPath := ExtractFilePath(ExtractFilePath(ParamStr(0))) + 'picture' +
    PathDelim + AModule;
  for Path in TDirectory.GetFiles(PicsPath, '*.bmp') do
  begin
    if FileExists(Path) then
    begin
      Bmp := TcxAlphaBitmap.Create;
      destBmp := TcxAlphaBitmap.Create;
      try
        Bmp.SetSize(ASourceSize, ASourceSize);
        Bmp.LoadFromFile(Path);
        destBmp := TcxAlphaBitmap.CreateSize(ADestSize, ADestSize);
        cxSmoothResizeBitmap(Bmp, destBmp, true);
        AImageList.Add(destBmp, nil);
      finally
        Bmp.Free;
        destBmp.Free;
      end;
    end
    else
      ShowMessage('No Image:' + Path);
  end;
end;

 

posted @ 2025-05-09 09:38  delphi中间件  阅读(38)  评论(0)    收藏  举报