procedure TForm1.Button3Click(Sender: TObject);
var
P: PByteArray;
x, y: Integer;
Gray: Integer;
Bmp: TBitmap;
begin
Bmp := TBitmap.Create;
Bmp.Assign(Image1.Picture.Bitmap);
Bmp.PixelFormat := pf24bit;
for y := 0 to Bmp.Height - 1 do
begin
P := Bmp.ScanLine[y];
for x := 0 to Bmp.Width - 1 do
begin
Gray := (P[3*x+2] + P[3*x+1] + P[3*x]) div 3;
P[3*x+2] := Byte(Gray);
P[3*x+1] := Byte(Gray);
P[3*x] := Byte(Gray);
end;
end;
Canvas.Draw(0, 0, Bmp);
Bmp.Free;
end;