试试下面的代码:procedure TForm1.Button1Click(Sender: TObject); var ABitmap : TBitmap; Dialog1 : TOpenDialog; Dialog2 : TSaveDialog; i, j : Integer; p : PByte; r, g, b : Byte; begin Dialog1 := TOpenDialog.Create(Self); try Dialog1.Filter := '*.bmp|*.bmp'; Dialog1.DefaultExt := '*.bmp'; if Dialog1.Execute then begin ABitmap := TBitmap.Create; try ABitmap.LoadFromFile(Dialog1.FileName); ABitmap.PixelFormat := pf24Bit; for i := 0 to ABitmap.Height - 1 do begin p := ABitmap.ScanLine[i]; for j := 0 to ABitmap.Width - 1 do begin r := p^; inc (p); g := p^; inc (p); b := p^; inc (p); if (r = 0) and (g = 0) and (b = 0) then begin dec (p, 3); p^ := $FF; inc (p); p^ := $FF; inc (p); p^ := $FF; inc (p); end; end; end; Dialog2 := TSaveDialog.Create(Self); try Dialog2.Filter := '*.bmp|*.bmp'; Dialog2.DefaultExt := '*.bmp'; if Dialog2.Execute then ABitmap.SaveToFile(Dialog2.Filename); finally Dialog2.Free; end; finally ABitmap.Free; end; end; finally Dialog1.Free; end; end;
一个很耗系统的方法:procedure ReMoveBitmapColor(var B: TBitmap; const OldColor, NewColor: TColor); var Col, Row: Integer; begin if OldColor = NewColor then exit; for Row := 0 to B.Height - 1 do for Col := 0 to B.Width - 1 do if B.Canvas.Pixels[ Col, Row ] = OldColor then B.Canvas.Pixels[ Col, Row ] := NewColor; end;
procedure TForm2.Button3Click(Sender: TObject); var B: TBitmap; begin B := TBitmap.Create; try B.Assign(Image1.Picture.Bitmap); ReMoveBitmapColor(Image1.Picture.Bitmap, clBlack, clwhite); Image1.Picture.Bitmap.Assign(b); finally B.Free; end; end;
var
ABitmap : TBitmap;
Dialog1 : TOpenDialog;
Dialog2 : TSaveDialog;
i, j : Integer;
p : PByte;
r, g, b : Byte;
begin
Dialog1 := TOpenDialog.Create(Self);
try
Dialog1.Filter := '*.bmp|*.bmp';
Dialog1.DefaultExt := '*.bmp';
if Dialog1.Execute then
begin
ABitmap := TBitmap.Create;
try
ABitmap.LoadFromFile(Dialog1.FileName);
ABitmap.PixelFormat := pf24Bit;
for i := 0 to ABitmap.Height - 1 do
begin
p := ABitmap.ScanLine[i];
for j := 0 to ABitmap.Width - 1 do
begin
r := p^;
inc (p);
g := p^;
inc (p);
b := p^;
inc (p);
if (r = 0) and (g = 0) and (b = 0) then
begin
dec (p, 3);
p^ := $FF;
inc (p);
p^ := $FF;
inc (p);
p^ := $FF;
inc (p);
end;
end;
end;
Dialog2 := TSaveDialog.Create(Self);
try
Dialog2.Filter := '*.bmp|*.bmp';
Dialog2.DefaultExt := '*.bmp';
if Dialog2.Execute then ABitmap.SaveToFile(Dialog2.Filename);
finally
Dialog2.Free;
end;
finally
ABitmap.Free;
end;
end;
finally
Dialog1.Free;
end;
end;
var
Col, Row: Integer;
begin
if OldColor = NewColor then exit;
for Row := 0 to B.Height - 1 do
for Col := 0 to B.Width - 1 do
if B.Canvas.Pixels[ Col, Row ] = OldColor then
B.Canvas.Pixels[ Col, Row ] := NewColor;
end;
var
B: TBitmap;
begin
B := TBitmap.Create;
try
B.Assign(Image1.Picture.Bitmap);
ReMoveBitmapColor(Image1.Picture.Bitmap, clBlack, clwhite);
Image1.Picture.Bitmap.Assign(b);
finally
B.Free;
end;
end;
1、3楼的方法比较适合PixelFormat=pf24Bit情况