Save Bitmap with transparency to PNG in Delphi 2007
Asked Answered
S

3

3

I have a Delphi bitmap (32Bit) that has transparency information. I need to convert and save it to a PNG file while preserving the transparency.

The tools I currently have are the graphics32 Library, GR32_PNG (by Christian Budde), and PNGImage (by Gustavo daud).

What is the best way to do this?

EDIT 1 : There is not just one color in my bitmap that's transparent but pixels with varying levels of transparency that needs to be preserved.

EDIT 2 : I am getting my bitmap with alpha information by setting the background of an image to a PNG I have with nothing but an empty alpha layer. Then I write anti-aliased text onto it. I then save the bitmap. When opened with gimp it show the transparency and upon zooming in you can see the effect of anti-aliasing.

Scan answered 12/1, 2011 at 15:30 Comment(0)
S
3

I could not get either of the other two answer to work. They may work for others but here is what I did.

  1. I created the bitmap with transparency.
  2. Assigned it to a TBitmap32 from the Graphics 32 library.
  3. Assigned it to a TPortableNetworkGraphics32 from Christian Budde's GR32_PNG for Graphics32
  4. Called the TPortableNetworkGraphics32.SaveToFile method
Scan answered 13/1, 2011 at 15:38 Comment(0)
C
4

Use PNGImage. In Delphi, try this:

function ConvertToPNG(oBMPSrc: TBitmap; sFilename: String);
var
  oPNGDest: TPNGObject;
begin
  oPNGDest := TPNGObject.Create;
  try
    oPNGDest.Assign(oBMPSrc);
    oPNGDest.SaveToFile(sFilename); 
  finally
    oPNGDest.Free;
  end;
end;

If that doesn't work for you, you'll probably have to copy over the alpha and RGB values individually.

function ConvertToPNG(oBMPSrc: TBitmap; sFilename: String);
var
  oPNGDest: TPNGObject;
begin
  oPNGDest := TPNGObject.CreateBlank(COLOR_RGBALPHA, 8, oBMPSrc.Width, oBMPSrc.Height);
  try
    oPNGDest.CreateAlpha;
    // Copy over RGB
    ..
    // Copy over Alpha
    ..
    // Save to file
    oPNGDest.SaveToFile(sFilename); 
  finally
    oPNGDest.Free;
  end;
end;
Cherin answered 12/1, 2011 at 18:2 Comment(1)
If I include the PNGImage unit in my program the saved bitmap I created (check EDIT 2 in main question) no longer shows any transparency information.Scan
S
3

I could not get either of the other two answer to work. They may work for others but here is what I did.

  1. I created the bitmap with transparency.
  2. Assigned it to a TBitmap32 from the Graphics 32 library.
  3. Assigned it to a TPortableNetworkGraphics32 from Christian Budde's GR32_PNG for Graphics32
  4. Called the TPortableNetworkGraphics32.SaveToFile method
Scan answered 13/1, 2011 at 15:38 Comment(0)
B
1

Check out this link

There is a function:

function WriteBitmapToPngFile( Filename : string; Bitmap : TBitmap; TransparentColor:TColor):boolean;
Biretta answered 12/1, 2011 at 17:16 Comment(1)
Looking at the code in the function it appears that it only makes 1 color transparent instead of the varying levels that I need. (have updated question to make this clear). However I still tried it by creating PNGUnit and PNGLib from the link provided. When calling the function the program starts to run but closes before it even shows the main form, without any error msg. Thank you for your help so far.Scan

© 2022 - 2024 — McMap. All rights reserved.