如何让彩色图片变成黑白?
以下代码可以使图片变成灰白,但我要的是黑白,请问怎么做?
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 := Max(P[3*x+2], P[3*x+1]);
Gray := Max(Gray, P[3*x]);
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;
[解决办法]
if Gray>127 then Gray:=255 else Gray:=0;
[解决办法]