Image Tiling in Delphi for Backgrounds and Textures

A short code snippet in Delphi which will do image tiling in Delphi which is useful when you want an application to have background texture.

By Tim TrottLegacy Code • February 6, 2004
Image Tiling in Delphi for Backgrounds and Textures

How to do Image Tiling in Delphi. This procedure will take a TImage instance loaded with a bitmap, and tile it over the area of the destination. Use for creating seamless background textures on forms or controls.

pascal
procedure TileImage(SourceImage, DestImage: TImage);
var
  XCnt, YCnt, X, Y: Integer;
  BevelSize, SaveIndex: Integer;
  Rect: TRect;
begin

  if (SourceImage.Picture.Graphic <> nil) and (SourceImage.Width > 0) and
    (SourceImage.Height > 0) then
  begin
    Rect := DestImage.ClientRect;
    //BevelSize := BorderWidth;
    //if BevelOuter <> bvNone then Inc(BevelSize, BevelWidth);
    //if BevelInner <> bvNone then Inc(BevelSize, BevelWidth);
    //InflateRect(Rect, -BevelSize, -BevelSize);
    SaveIndex := SaveDC(DestImage.Canvas.Handle);
    try
      IntersectClipRect(DestImage.Canvas.Handle, Rect.Left, Rect.Top,
        Rect.Right - Rect.Left + 1,
        Rect.Bottom - Rect.Top + 1);
      XCnt := DestImage.ClientWidth div SourceImage.Width;
      YCnt := DestImage.ClientHeight div SourceImage.Height;
      for X := 0 to XCnt do
        for Y := 0 to YCnt do
          DestImage.Canvas.Draw(Rect.Left + X * SourceImage.Width,
            Rect.Top + Y * SourceImage.Height, SourceImage.Picture.Graphic);
    finally
      RestoreDC(DestImage.Canvas.Handle, SaveIndex);
    end;
  end;
end;

About the Author

Tim Trott is a senior software engineer with over 20 years of experience in designing, building, and maintaining software systems across a range of industries. Passionate about clean code, scalable architecture, and continuous learning, he specialises in creating robust solutions that solve real-world problems. He is currently based in Edinburgh, where he develops innovative software and collaborates with teams around the globe.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

New comments for this post are currently closed.