Firemonkey PNG to bitmap

1.4k views Asked by At

In firemonkey xe5, when I save a transparent PNG image into bitmap its transparency become black. How can I change into white?

I just use:

Image1.bitmap.loadfromfile('IMG.png');
image1.bitmap.Savetofile('image.BMP');
1

There are 1 answers

1
FMXExpress On

This isn't a complete solution but it will get you started. You will need to tweak the A, R, G, B values to get what you want.

procedure ApplyNoAlphaEdge(ABitmap: TBitmap; OpacityThreshold: integer);
var
  bitdata1: TBitmapData;
  I: integer;
  J: integer;
  C: TAlphaColor;
begin
  if (ABitmap.Map(TMapAccess.maReadWrite, bitdata1)) then
    try
      for I := 0 to ABitmap.Width - 1 do
        for J := 0 to ABitmap.Height - 1 do
        begin
          begin
              {$IF DEFINED(VER270) OR DEFINED(VER280) OR DEFINED(VER290)}
              C := PixelToAlphaColor(@PAlphaColorArray(bitdata1.Data)
              [J * (bitdata1.Pitch div PixelFormatBytes[ABitmap.PixelFormat])
               + 1 * I], ABitmap.PixelFormat);
              {$ELSE}
              C := PixelToAlphaColor(@PAlphaColorArray(bitdata1.Data)
              [J * (bitdata1.Pitch div GetPixelFormatBytes(ABitmap.PixelFormat))
               + 1 * I], ABitmap.PixelFormat);
              {$ENDIF}
               if TAlphaColorRec(C).A<OpacityThreshold then
                begin
                  TAlphaColorRec(C).A := 0;
                  TAlphaColorRec(C).R := 255;
                  TAlphaColorRec(C).G := 255;
                  TAlphaColorRec(C).B := 255;

              {$IF DEFINED(VER270) OR DEFINED(VER280) OR DEFINED(VER290)}
                  AlphaColorToPixel(C, @PAlphaColorArray(bitdata1.Data)
                    [J * (bitdata1.Pitch div PixelFormatBytes[ABitmap.PixelFormat])
                     + 1 * I], ABitmap.PixelFormat);
              {$ELSE}
                  AlphaColorToPixel(C, @PAlphaColorArray(bitdata1.Data)
                    [J * (bitdata1.Pitch div GetPixelFormatBytes(ABitmap.PixelFormat))
                     + 1 * I], ABitmap.PixelFormat);
              {$ENDIF}
                end;
          end;
        end;
    finally
      ABitmap.Unmap(bitdata1);
    end;
end;