本例效果图:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Image1: TImage;
    procedure FormCreate(Sender: TObject);
    procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

const
  TargetColor: TColor = clRed;
var
  SeedColor: TColor;
  x1,y1: Integer;
  pic: TPicture;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Image1.AutoSize := True;
  Image1.Picture.LoadFromFile('c:\temp\test.bmp');
  x1 := Image1.Left;
  y1 := Image1.Top;
  pic := Image1.Picture;
  Button1.Caption := '恢复';
end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  w,h: Integer;
  ImgHdc: HDC;
begin
  ImgHdc := Image1.Canvas.Handle;
  SeedColor := GetPixel(ImgHdc, X,Y);

  for w := 0 to Image1.Width-1 do
  for h := 0 to Image1.Height-1 do
    if GetPixel(ImgHdc, w,h)=SeedColor then
      SetPixelV(Canvas.Handle, x1+w, y1+h, TargetColor);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Image1.Picture := pic;
end;

end.

posted on 2008-03-21 15:47  万一  阅读(2695)  评论(3编辑  收藏  举报