Scroll image with ScrollBars

Posted in Components

If your picture has very large width or height, then you may use ScrollBars for scrolling of this picture. For example, ScrollBar1 will change a horizontal position of this picture and ScrollBar2 will change a vertical position of this picture. Set OnChange event to Scrollbar2Change for each ScrollBar.

var
  MyBitmap: TBitmap;

...

procedure TForm1.ScrollBar2Change(Sender: TObject);
var
  RectDest, RectSource: TRect;
begin
  RectDest:=Rect(0, 0, Image1.Width, Image1.Height);
  RectSource:=Rect(
    ScrollBar1.Position, 
    ScrollBar2.Position, 
    Scrollbar1.Position+Image1.Width, 
    ScrollBar2.Position+Image1.Height);
  Image1.Canvas.CopyRect(RectDest, MyBitmap.Canvas, RectSource);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyBitmap:=TBitmap.Create;
  MyBitmap.LoadFromFile('factory.bmp');
  Image1.Picture.Bitmap.Assign(MyBitmap);
  ScrollBar1.Max:=MyBitmap.Width-1-Image1.Width;
  ScrollBar2.Max:=MyBitmap.Height-1-Image1.Height;
end;