Search code examples
delphidelphi-2009vcltimage

How to tile a Image in TImage?


How do I tile an image in a TImage in Delphi?

Why I need it: Instead of creating more TImages at runtime, I could create one and store my image there knowing that it will be 'fit' until it reaches TImage's height and width.

Please suggest any ideas to do this.

Thank you!

EDIT: Please note, I'm not asking for streching the image, but filling the canvas by repeating the image.


Solution

  • The following is the function that I have used, taking an existing TImage component and tiling it over a target canvas:

    procedure TileImage(const Source:tImage;
        Target: TCanvas;
        TargetHeight,TargetWidth:integer);
    // Tiles the source image over the given target canvas
    var
      X, Y: Integer;
      dX, dY: Integer;
    begin
      dX := Source.Width;
      dY := Source.Height;
      Y := 0;
      while Y < TargetHeight do
        begin
          X := 0;
          while X < TargetWidth do
            begin
              Target.Draw(X, Y, Source.Picture.graphic);
              Inc(X, dX);
            end;
          Inc(Y, dY);
        end;
    end;
    

    Because a tLabel exposes a canvas, you can do tricks like the following:

    TileImage(Image1,Label1.Canvas,Label1.Height,Label1.Width);