对于24色位图:
procedure TMainForm.GraychangeClick(Sender: TObject);
var
p: PByteArray;
x, y: Integer;
Bmp: TBitmap;
Gray: byte;
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 := Round(p[x * 3 + 2] * 0.3 + p[x * 3 + 1] * 0.59 + p[x
* 3]
* 0.11);
{ TODO : 灰度常用算法 }
p[x * 3] := Gray;
p[x * 3 + 1] := Gray;
p[x * 3 + 2] := Gray;
end
end;
Image1.Picture.Bitmap.Assign(Bmp);
Bmp.Free;
end;