在image加載的圖片上畫矩形,會將圖片覆蓋,代碼如下:
unit Unit3;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls;
type
TForm3 = class(TForm)
img1: TImage;
dlgOpen1: TOpenDialog;
procedure FormCreate(Sender: TObject);
procedure img1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure img1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure img1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
var
cx, cy: Integer;
md: Boolean;
procedure TForm3.FormCreate(Sender: TObject);
var
openPath: string;
begin
dlgOpen1.Title := '打開圖片';
dlgOpen1.Filter := '全部 (*.jpg;*.jpeg;*.bmp;)';
if dlgOpen1.Execute then
begin
openPath := dlgOpen1.FileName;
img1.Picture.LoadFromFile(openPath);
img1.AutoSize := False;
img1.Stretch := True;
end;
Form3.DoubleBuffered := true;
md := False;
end;
procedure TForm3.img1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
md := True;
cx := x;
cy := y;
end;
procedure TForm3.img1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
if md then
begin
// img1.Canvas.Brush.Color := clWhite; //畫筆顏色設置為白色
// img1.Canvas.Brush.Style := bsclear;
//img1.Canvas.FillRect(img1.ClientRect); //把畫布背景填充為白色
img1.Canvas.Pen.Color := clRed;
img1.Canvas.Rectangle(cx, cy, X, Y);
end;
end;
procedure TForm3.img1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
md := False;
end;
end.
使用FrameRect 用指定的刷子圍繞一個矩形畫一個邊框。
procedure TForm3.img1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
if md then
begin
img1.Canvas.Brush.Color := clWhite; //畫筆顏色設置為白色
img1.Canvas.Rectangle(cx, cy, X, Y);
Img1.Canvas.FrameRect(TheRect);
end;
end;