Robert Fergusson
Robert Fergusson

Reputation: 97

Overlaying pictures in one image

Is it possible to merge two or more different bmp-pictures of the same size into one by overlaying on top of each other? The same way it was done in Windows XP MS Paint: pasting one picture in another, with secondary color being transparent.

Example

Upvotes: 7

Views: 4722

Answers (2)

NGLN
NGLN

Reputation: 43664

In case of the second bitmap is black-and-white, you can use it as a mask in a raster operation with BitBlt ( bit-block transfer), as follows:

  Windows.BitBlt(Bmp3.Canvas.Handle, 0, 0, Bmp3.Width, Bmp3.Height,
    Bmp1.Canvas.Handle, 0, 0, SRCCOPY);
  Windows.BitBlt(Bmp3.Canvas.Handle, 0, 0, Bmp3.Width, Bmp3.Height,
    Bmp2.Canvas.Handle, 0, 0, SRCAND);

Upvotes: 4

Sertac Akyuz
Sertac Akyuz

Reputation: 54832

You can use Transparent property of TBitmap to that effect. Since your bitmaps have a black border, automatic transparent color (first pixel of image data) wouldn't work and you need to also set the TransparentColor property to 'clWhite'.

var
  bmp1, bmp2: TBitmap;
begin
  bmp1 := TBitmap.Create;
  bmp1.LoadFromFile('...\test1.bmp');

  bmp2 := TBitmap.Create;
  bmp2.LoadFromFile('...\test2.bmp');

//  bmp2.PixelFormat := pf24bit;  // with 32 bit images I need this, don't know why
  bmp2.Transparent := True;
  bmp2.TransparentColor := clWhite;
  bmp1.Canvas.Draw(0, 0, bmp2);  // draw bmp2 over bmp1

  // this is how the merged image looks like
  Canvas.Draw(0, 0, bmp1);
  ..

Upvotes: 11

Related Questions