Change alpha data per pixel for TPngImage

Can I directly modify alpha data per pixel for TPngImage between loading and retrieving it? If so, how? Thank.

+5
source share
2 answers

Yes, I think it is easy.

For example, this code sets the opacity to zero (i.e., transparency up to 100%) for each pixel in the upper half of the image:

var
  png: TPNGImage;
  sl: PByteArray;

...

for y := 0 to png.Height div 2 do
begin
  sl := png.AlphaScanline[y];
  FillChar(sl^, png.Width, 0);
end;

This will lead to the alpha channel of the linear gradient from full transparency (alpha = 0) to full opacity (alpha = 255) from left to right:

for y := 0 to png.Height do
begin
  sl := png.AlphaScanline[y];
  for x := 0 to png.Width - 1 do
    sl^[x] := byte(round(255*x/png.Width));
end;

Basically, I'm trying to say that

(png.AlphaScanline[y]^)[x]

is the alpha value (opacity) as a pixel byte in the string y and col x.

+8
source

- :

for Y := 0 to Image.Height - 1 do begin
  Line := Image.AlphaScanline[Y];
  for X := 0 to Image.Width - 1 do begin        
      Line[X] := ALPHA        
  end;
end;
+3

All Articles