Mega Code Archive

 
Categories / Delphi / Graphic
 

Another way to draw tile image to a canvas

Title: Another way to draw tile image to a canvas Question: 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 x := 0; tx := 1; while (tx 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