C++Builder Logo
How to tile a bitmap in a canvas Afficher un bitmap dans un canvas sous forme d'une mosaïque

This code snippet tiles a bitmap (here in the file comp.bmp) onto a canvas. In this example, the canvas is a TImage canvas. 
Of course, you may use another canvas if you want.
Le code ci-dessous, affiche un bitmap dans un canvas sous forme d'une mosaïque. Le canvas utilisé dans cet exemple est le canvas d'un TImage. Vous pouvez, bien sur, utilisé un autre canvas.

Graphics::TBitmap *tmpBitmap=new Graphics::TBitmap(); 
tmpBitmap->LoadFromFile("comp.bmp");     //Load bitmap from file 
TRect rect; 
int i,j,bmpWidth,bmpHeight; 

rect=Image1->ClientRect;                 //Get size of TImage 
for(i=rect.Top;i<rect.Bottom;i+=tmpBitmap->Height) 

  for(j=rect.Left;j<rect.Right;j+=tmpBitmap->Width) 
  { 
    if(j<rect.Right-tmpBitmap->Width)bmpWidth=tmpBitmap->Width; 
    else bmpWidth=rect.Right-j; 
    if(i<rect.Bottom-tmpBitmap->Height)bmpHeight=tmpBitmap->Height; 
    else bmpHeight=rect.Bottom-i; 
      ::BitBlt(Image1->Canvas->Handle,   //Handle of dest. device context 
               j,                        //x coord. of dest. upper-left corner 
               i,                        //y coord. of dest. upper-left corner 
               bmpWidth,                 //width of dest. rectangle 
               bmpHeight,                //height of dest. rectangle 
               tmpBitmap->Canvas->Handle,//Handle of source device context 
               0,                        //x coord of source upper-left corner 
               0,                        //y coord of source upper-left corner 
               SRCCOPY);                 //Specifies how to draw 
  } 

delete tmpBitmap;