Website development and design blog, tutorials and inspiration

Image Tiling in Delphi

Image Tiling in Delphi

Written By on in Software Engineering 0

Image Tiling in Delphi

133 words, estimated reading time 1 minutes.

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.

  1. procedure TileImage(SourceImage, DestImage: TImage);
  2. var
  3. XCnt, YCnt, X, Y: Integer;
  4. BevelSize, SaveIndex: Integer;
  5. Rect: TRect;
  6. begin
  7.  
  8. if (SourceImage.Picture.Graphic <> nil) and (SourceImage.Width > 0) and
  9. (SourceImage.Height > 0) then
  10. begin
  11. Rect := DestImage.ClientRect;
  12. //BevelSize := BorderWidth;
  13. //if BevelOuter <> bvNone then Inc(BevelSize, BevelWidth);
  14. //if BevelInner <> bvNone then Inc(BevelSize, BevelWidth);
  15. //InflateRect(Rect, -BevelSize, -BevelSize);
  16. SaveIndex := SaveDC(DestImage.Canvas.Handle);
  17. try
  18. IntersectClipRect(DestImage.Canvas.Handle, Rect.Left, Rect.Top,
  19. Rect.Right - Rect.Left + 1,
  20. Rect.Bottom - Rect.Top + 1);
  21. XCnt := DestImage.ClientWidth div SourceImage.Width;
  22. YCnt := DestImage.ClientHeight div SourceImage.Height;
  23. for X := 0 to XCnt do
  24. for Y := 0 to YCnt do
  25. DestImage.Canvas.Draw(Rect.Left + X * SourceImage.Width,
  26. Rect.Top + Y * SourceImage.Height, SourceImage.Picture.Graphic);
  27. finally
  28. RestoreDC(DestImage.Canvas.Handle, SaveIndex);
  29. end;
  30. end;
  31. end;

Last updated on: Saturday 24th June 2017

Did you Like this Post? Why not Like us on Facebook?

 

Comments

There are no comments for this post. Be the first!

Leave a Reply

Your email address will not be published.