How to save a PNGImage mask when loading from a resource using Delphi XE

I upgrade my code to Delphi XE (from Delphi 7) and try to eliminate all unnecessary libraries. I've been using PNGComponents for ages, but it's time to move on and use my own TImageList and TPNGImage.

Part of my code loads a list of images at runtime from related resources. My working PNGComponents code for this:

function CreateAndLoadImageList( ASize : integer ) : TPngImageList;
var
  PngObject : TPngObject;
  I : integer;
begin
  Result := TPngImageList.Create( nil );

  Result.BeginUpdate;
  try

    Result.Width := ASize;
    Result.Height := ASize;

    PngObject := TPngObject.create;
    try

      For I := 0 to Length( ArtImageNames ) -1 do
        begin
        PngObject.LoadFromResourceName( hInstance, Format( 'AImg%d_%d', [ASize, I]));

        Result.PngImages.Add( False).PngImage := PngObject;
        end;

    finally
      PngObject.Free;
    end;


  finally
    Result.EndUpdate;
  end;

end;

Using the answer to this question Now I'm trying to use code that shows images, but with a black background, apparently because the mask is lost. I think I need to get a bitmap of the mask to go to ImageList_Add, where "0", but I am poor from this stuff. Does anyone know how I can make this work?

function CreateAndLoadImageList( ASize : integer ) : TImageList;
var
  PngImage : TPngImage;
  bmp : TBitmap;
  I : integer;
begin

  Result := TImageList.Create( nil );
  Result.Masked := False;
  Result.DrawingStyle := dsTransparent;

  Result.BeginUpdate;
  try

    Result.Width := ASize;
    Result.Height := ASize;
    Result.Masked := False;

    PngImage := TPngImage.create;
    try

      For I := 0 to Length( ArtImageNames ) -1 do
        begin
        PngImage.LoadFromResourceName( hInstance, Format( 'AImg%d_%d', [ASize, I]));


        bmp:=TBitmap.Create;
        PngImage.AssignTo(bmp);

        bmp.AlphaFormat:=afIgnored;

        ImageList_Add( Result.Handle, bmp.Handle, 0);
        Bmp.Free;

        end;
    finally
      PngImage.Free;
    end;


  finally
    Result.EndUpdate;
  end;

end;
+5
1

PNG -. . , , .

  • ColorDepth cd32Bit.
  • , , PNG, , AlphaFormat.

, , Assign, AssignTo. AssignTo - , TPersistent.

+2

All Articles