TWICImage: how to save transparent BMP?
01:42 22 Sep 2021

I'm using Delphi 10.4.2 and I have an application that scales images (BMP, GIF, PNG, ...).

My problem is that if the input image is a transparent BMP the output BMP is not transparent, instead it has a black backgound. This also happens when the input is a transparent PNG and I want to output a BMP. How can I avoid that?

Sample code:

procedure TFrmTestGenImg.Resize;
var
  LInputImgPath, LOutputImgPath: string;
  LImageIn, LImageOut: TWICImage;
  LBitmap: TBitmap;
begin
  LInputImgPath := 'C:\temp\Input.bmp';

  LOutputImgPath := 'C:\temp\Output.bmp';

  LImageIn := TWICImage.Create;
  try
    LImageOut := TWICImage.Create;
    try
      LImageIn.Transparent := True;
      LImageIn.LoadFromFile(Trim(LInputImgPath));

      LBitmap := TBitmap.Create;
      try
        LBitmap.PixelFormat := pf32bit;
        LBitmap.Assign(LImageIn);
        LBitmap.Transparent := True;
        { ... Resizing bitmap ... }

        LImageOut.Assign(LBitmap);
        LImageOut.ImageFormat := wifBmp;
        LImageOut.Transparent := LImageIn.Transparent;
        LImageOut.SaveToFile(LOutputImgPath);
      finally
        LBitmap.Free;
      end;
    finally
      LImageOut.Free;
    end;
  finally
    LImageIn.Free;
  end;
end;
image delphi delphi-10.4-sydney wic delphi-10.4.2