I have a png that i would like to load in a TBitmap32.
After I load the bitmap I call:
Bitmap.DrawMode := dmTransparent;
Bitmap.OuterColor := Bitmap.PixelS[0,0];
But then all white pixels are transparent. How can i do that just for the transparent part of the png image? Here is my image with the alpha transparency around the edge of the image indicated in the standard way.
And this is the actual image:
It seems that
TBitmap32
may lose alpha information while loading a PNG image.You may consider to use the GR32PNG library of which a documentation excerpt follows:
Graphics32 project page
The code usage example of the unit can be changed as below to suit your needs:
Where
Bitmap
is aTBitmap32
object.The resulting image loaded in a form within a
TImage32
component:The problem may be that the PNG has incorrectly converted to TBitmap32, losing the transparency information in transit. It is a common case with paletted PNG images. Otherwise, you would not had to use „Bitmap.DrawMode := dmTransparent” and the „OuterColor”. If the transparencry information from PNG would have correctly transferred to TBitmpa32, DrawMode := dmBlend would have worked, without the need to set the OuterColor.
What matters most is how did you load a PNG into the TBitmap32. The TPngImage from Vcl.Imaging.pngimage unit (implemented in Delphi XE2 and later) can draw transparently on bitmaps, preserving what was on that bitmaps, combining colors using the PNG alpha layer, etc, but it doesn’t allow to easily convert various formats of PNG transparency (including paletted) into the alpha component of each pixel of TBitmap32. Once TPngImage have drawn an image, you get the combined RGB for each pixel, but the alpha component is not transferred to the target bitmap.
There are helper routines available that try to load a PNG into a TBitmap32 with transparency, but they have drawbacks:
(1) “LoadPNGintoBitmap32” from http://graphics32.org/wiki/FAQ/ImageFormatRelated - it applies the transparency twice, so the images with alpha values other than 0 or 255 will look differently than in other software (most noticeable on translucent images with glass effects). This code will first apply alpha to RGB and then sets alpha as a separate layer, so when you paint, alpha will be applied again. You can find more information on this issue here: Delphi, GR32 + PngObject: converting to Bitmap32 doesn't work as expected . Besides that, it doesn't convert correctly transparency from paletted images into the alpha layer of TBitmap32. They manually set alpha transparency for the pixels of a certain color of the output bitmap (rendered to RGB) rather doing that before rendering to RGB, so the actual transparency is lost as on your sample image when all white pixels are transparent.
(2) “LoadBitmap32FromPNG” from gr32ex library: https://code.google.com/archive/p/gr32ex/ - a slightly different implementation of the same algorithm as (1), and has the same issues as (1).
So, the solutions are:
Since you now have all the information on this issue, you may choose the right solution for you.