我有一个bmp图片,有5-6种颜色组成,我想将黑色颜色部分变成白色,怎么才可以实现????

解决方案 »

  1.   

    试试下面的代码: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;
      

  2.   

    一个很耗系统的方法: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;
      

  3.   

    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;
      

  4.   

    “bmp图片,有5-6种颜色组成”--那么PixelFormat=pf4Bit还是PixelFormat=pf8Bit?
    1、3楼的方法比较适合PixelFormat=pf24Bit情况