unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtDlgs, StdCtrls, ExtCtrls, Menus;type
TForm1 = class(TForm)
Image1: TImage;
Button1: TButton;
dlgOpenPic1: TOpenPictureDialog;
pm1: TPopupMenu;
N1: TMenuItem;
SavePictureDialog1: TSavePictureDialog;
procedure Button1Click(Sender: TObject);
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure N1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
SourceRect,DescRect:TRect;
draw:Boolean;
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
if dlgOpenPic1.Execute then
Image1.Picture.LoadFromFile(dlgOpenPic1.FileName);
end;procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if button=mbleft then
begin
image1.Canvas.DrawFocusRect(SourceRect);
SourceRect.Left:=x;
SourceRect.top:=y;
SourceRect.right:=x;
SourceRect.Bottom:=y;
draw:=true;
end;
end;procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if button=mbleft then
begin
SourceRect.right:=x;
SourceRect.Bottom:=y;
DescRect.top:=0;
DescRect.Left:=0;
DescRect.Right:=SourceRect.Right-SourceRect.Left;
DescRect.Bottom:=SourceRect.Bottom-SourceRect.Top;
//image2.Canvas.CopyRect(DescRect,image1.canvas,SourceRect) ;
draw:=false;
end;end;procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if draw then
begin
image1.Canvas.DrawFocusRect(SourceRect);
SourceRect.Right:=x;
SourceRect.bottom:=y;
image1.Canvas.DrawFocusRect(SourceRect);
end;end;procedure TForm1.N1Click(Sender: TObject);
var
DescImg:TImage;
begin
try
DescImg := Timage.Create(application);
DescImg.Height :=DescRect.Bottom;
descImg.Width :=DescRect.Right;
DescImg.Canvas.CopyRect(DescRect,image1.Canvas,SourceRect);
if SavePictureDialog1.Execute then
descImg.Picture.SaveToFile(SavePictureDialog1.FileName+'.bmp');
Finally
freeandnil(descImg);
end;
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtDlgs, StdCtrls, ExtCtrls, Menus;type
TForm1 = class(TForm)
Image1: TImage;
Button1: TButton;
dlgOpenPic1: TOpenPictureDialog;
pm1: TPopupMenu;
N1: TMenuItem;
SavePictureDialog1: TSavePictureDialog;
procedure Button1Click(Sender: TObject);
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure N1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
SourceRect,DescRect:TRect;
draw:Boolean;
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
if dlgOpenPic1.Execute then
Image1.Picture.LoadFromFile(dlgOpenPic1.FileName);
end;procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if button=mbleft then
begin
image1.Canvas.DrawFocusRect(SourceRect);
SourceRect.Left:=x;
SourceRect.top:=y;
SourceRect.right:=x;
SourceRect.Bottom:=y;
draw:=true;
end;
end;procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if button=mbleft then
begin
SourceRect.right:=x;
SourceRect.Bottom:=y;
DescRect.top:=0;
DescRect.Left:=0;
DescRect.Right:=SourceRect.Right-SourceRect.Left;
DescRect.Bottom:=SourceRect.Bottom-SourceRect.Top;
//image2.Canvas.CopyRect(DescRect,image1.canvas,SourceRect) ;
draw:=false;
end;end;procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if draw then
begin
image1.Canvas.DrawFocusRect(SourceRect);
SourceRect.Right:=x;
SourceRect.bottom:=y;
image1.Canvas.DrawFocusRect(SourceRect);
end;end;procedure TForm1.N1Click(Sender: TObject);
var
DescImg:TImage;
begin
try
DescImg := Timage.Create(application);
DescImg.Height :=DescRect.Bottom;
descImg.Width :=DescRect.Right;
DescImg.Canvas.CopyRect(DescRect,image1.Canvas,SourceRect);
if SavePictureDialog1.Execute then
descImg.Picture.SaveToFile(SavePictureDialog1.FileName+'.bmp');
Finally
freeandnil(descImg);
end;
end;end.
具体去看Help
我意思是:因为上面的事件都是围Image来操作的,所以想在image基础上做一个特殊的Image控件。
严重同意