TPNGImage help

PNG Delphi

Accessing thru AlphaAscanline


The AlphaScanline property contains only a valid value when the color mode for the current image is either COLOR_RGBALPHA or COLOR_GRAYSCALEALPHA. You should check it before accessing it.

It's real simple to use this way. AlphaScanline is an indexed property that returns a pointer to a byte array containing transparency information for needed image line. So, it means that the return is a pByteArray which is type ^Array[Word] of Byte for the line. Also the returned array contains one byte for each line pixel, so the array bounds is 0 to ImageWidth - 1

The example shows how to make the current image (don't work for COLOR_PALETTE mode) half transparent:

procedure MakeImageHalfTransparent(Obj: TPNGObject);
var i, j: integer;
begin
 
//Add alpha channel in case it is RGB or GRAYSCALE
  if
Obj.Header.ColorType in [COLOR_RGB, COLOR_GRAYSCALE] then
   
Obj.CreateAlpha();
 
 
//Set half transparent transparency, value 128 (256 / 2)
  if
Obj.Header.ColorType in [COLOR_RGBALPHA, COLOR_GRAYSCALEALPHA] then
    FOR j := 0 TO Obj.Header.Height - 1 DO
      FOR i := 0 TO Obj.Header.Width - 1 DO
        Obj.AlphaScanline[j]^[i] := 128
end;