CreateBitmap, Size, and Combine example for Delphi

This example creates a bitmap for Lead2 (same size and BPS as Lead1) and fills it with green. It resizes the Lead1 bitmap, then uses a loop to do multiple combines with the Lead2 bitmap. It then copies the Lead2 bitmap to Lead1 and redisplays Lead1.

var
   IntMyWidth, IntMyHeight, IntImageWidth, IntImageHeight, MyOp, MyBPS: Integer;
   MyWidth, MyHeight, ImageWidth, ImageHeight, VFactor, HFactor: Single;
   TestWidth, TestHeight, VSpacing, HSpacing: Integer;
   SrcL, SrcT, DstT, DstL, DstW, DstH: Single;
begin
Screen.Cursor:= crHourGlass;
{ Create the Lead2 bitmap. (The control must already be on the form).}
MyWidth:= Lead1.BitmapWidth;
MyHeight:= Lead1.BitmapHeight;
MyBPS:= Lead1.BitmapBits;
Lead2.CreateBitmap(Round(MyWidth), Round(MyHeight), MyBPS);
Lead2.Fill(RGB(0, 255, 0)); { fill with green}
{ Resize the Lead1 bitmap}
ImageWidth:= round(MyWidth * 0.3);
ImageHeight:= round(MyHeight * 0.3);
Lead1.Size(Round(ImageWidth), Round(ImageHeight), SIZE_RESAMPLE); 
{ Initialize the variables used for multiple combines.}
TestWidth:= round(MyWidth); { Used for horizontal positioning}
TestHeight:= round(MyHeight); { Used for vertical positioning}
IntMyWidth:= round(MyWidth);  { Green bitmap width}
IntMyHeight:= round(MyHeight); { Green bitmap height}
IntImageWidth:= round(ImageWidth); { Resized bitmap width}
IntImageHeight:= round(ImageHeight); { Resized bitmap height}
VFactor:= round(MyHeight / ImageHeight) + 1; { Number spaces between images}
VSpacing:= round((IntMyHeight MOD IntImageHeight) / VFactor); { Pixels per space}
HFactor:= round(ClientWidth / ImageWidth) + 1; { Number spaces between images}
HSpacing:= round((IntMyWidth MOD IntImageWidth) / HFactor); { Pixels per space}
SrcL:= 0; { Left coordinate of the source rectangle}
SrcT:= 0; { Top coordinate of the source rectangle}
DstW:= ImageWidth;  { Width of the destination rectangle}
DstH:= ImageHeight; { Height of the destination rectangle}
MyOp:= CB_OP_ADD or CB_DST_0; { Operation flags used when combining images}
{ Do the loop that does the multiple combines}
While TestHeight > IntImageHeight do { Vertical loop}
begin
   DstT:= IntMyHeight - TestHeight + VSpacing;
   TestHeight:= TestHeight - IntImageHeight - VSpacing;
   While TestWidth > IntImageWidth do {Horizontal loop}
   begin
      DstL:= IntMyWidth - TestWidth + HSpacing;
      TestWidth:= TestWidth - IntImageWidth - HSpacing;
      Lead2.Combine(Round(DstL), Round(DstT), Round(DstW), Round(DstH), Lead1.Bitmap, Round(SrcL), Round(SrcT), MyOp);
   end;
   TestWidth:= IntMyWidth;
end; 
{ Copy the Lead2 bitmap to Lead1}
Lead1.Bitmap:= Lead2.Bitmap;
Lead2.Bitmap := 0; 
{Set the image display size to match the LEAD control}
Lead1.SetDstRect(0, 0, Lead1.BitmapWidth, Lead1.BitmapHeight);
Lead1.SetDstClipRect(0, 0, Lead1.BitmapWidth, Lead1.BitmapHeight);
Lead1.ForceRepaint; { Repaint the image}
Screen.Cursor:= crDefault;
end;