unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ScrollBox1: TScrollBox;
    Image1: TImage;
    Label1: TLabel;
    Label2: TLabel;
    Image2: TImage;
    Button2: TButton;
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
   StartX, StartY, StopX, StopY: Integer;
   bmppom:TBitmap;
implementation

{$R *.dfm}


procedure TForm1.Button2Click(Sender: TObject);
var i,j,w,h:integer;
begin
  w:=ABS(StopX-StartX);
  h:=ABS(StopY-StartY);
  Image2.Picture.Bitmap.Width:=w;
  Image2.Picture.Bitmap.Height:=h;

  if StartX>StopX then StartX:=StopX;
  if StartY>StopY then StartY:=StopY;
  for i := 0 to w-1 do
    for j := 0 to h-1 do
    Image2.Picture.Bitmap.Canvas.Pixels[i,j]:=
    Image1.Picture.Bitmap.Canvas.Pixels[StartX+i,StartY+j];

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  bmppom:=TBitmap.Create;
  bmppom.Assign(Image1.Picture.Bitmap);
end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
    StartX := X;
    StartY := Y;
end;

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if x<0 then x:=0;
  if y<0 then y:=0;
  if x>Image1.ClientWidth then x:=Image1.ClientWidth;
  if y>Image1.ClientHeight then y:=Image1.ClientHeight;

  with Image1 do
  if ssLeft in Shift then //czy lewy myszy przycisk wcisniety
  begin
    Image1.Picture.Bitmap:=bmppom; //odtworzenie poprzedniej bitmapy
    Image1.Canvas.Pen.Color:=clred;
    Image1.Canvas.Brush.Style:=bsclear;;

    Image1.Picture.Bitmap.Canvas.Rectangle(StartX, StartY, X, Y);
    StopX:=X;StopY:=Y;
    //wyświetlanie współrzędnych dwóch przeciwległych wierzchołków
    if X > StartX then //rysowanie w prawo od punktu startowego
    begin
      Label1.Caption:= ' Left: ' + IntToStr(StartX);
      Label2.Caption:= ' Right: ' + IntToStr(X);
    end;
    if X<=StartX then //else
    begin //rysowanie w lewo od punktu startowego
      Label1.Caption:=' Left: ' + IntToStr(X);
      Label2.Caption:= ' Right: ' + IntToStr(StartX);
    end;

    if Y > StartY then //rysowanie w dół od punktu startowego
    begin
      Label1.Caption:=Label1.Caption+' Top: ' +
                                             IntToStr(StartY);
      Label2.Caption:=Label2.Caption+' Bottom: ' + IntToStr(Y);
    end
    else
    begin //rysowanie w górę od punktu startowego
      Label1.Caption :=Label1.Caption+ ' Top: ' + IntToStr(Y);
      Label2.Caption :=Label2.Caption+ ' Bottom: ' +
                                  IntToStr(StartY);
    end;  
end;
end;

end.
