:: My another contributions in Delphi

»Delphi3000.com
posting and uploading delphi articles
[currently as Top 66 uploader]


»Komunitas Delphi Indonesia
starting as a member

Saturday, February 03, 2007

Another Way to Draw Tiled Image to a Canvas

:: Counted Tile Image Draw ::

Question/Problem/Abstract:

How we can get a counted tiling image on a canvas ?
counted as TileX (Horizontally = columns) and TileY(Vertically = lines)
Eg: TileX = 3, TileY = 3, so we can get a 3x3 tiled image on a canvas

Answer:

procedure CountedTileImage(const aPicture: TPicture;
const aTileX, aTileY: Integer; destCanvas: TCanvas;
const destWidth, destHeight: Integer);
var x, y, tx, ty: integer;
src, dst: TBitmap;
myRect: TRect;
begin
if (not aPicture.Graphic.Empty) and (aPicture.Graphic <> nil) then
begin
src := TBitmap.Create;
try
src.Assign(aPicture.Graphic);
dst := TBitmap.Create;
try
dst.Width := aTileX * aPicture.Graphic.Width;
dst.Height := aTileY * aPicture.Graphic.Height;
y := 0;
ty := 1;
while (ty <= aTileY) do begin
x := 0;
tx := 1;
while (tx <= aTileX) do begin
myRect := Rect(x, y, x+src.Width, y+src.Height);
dst.Canvas.CopyRect(myRect, src.Canvas, Rect(0, 0, src.Width, src.Height));
x := x + src.Width;
tx := tx + 1;
end;
y := y + src.Height;
ty := ty + 1;
end;
finally
destCanvas.StretchDraw(Rect(0,0,destWidth,destHeight), dst);
dst.Free;
end;
finally
src.Free;
end;
end;
end;


Have a good time