Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlaying pictures in one image

Tags:

image

delphi

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

like image 989
Robert Fergusson Avatar asked Sep 22 '13 22:09

Robert Fergusson


2 Answers

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);
  ..
like image 95
Sertac Akyuz Avatar answered Oct 28 '22 00:10

Sertac Akyuz


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);
like image 20
NGLN Avatar answered Oct 28 '22 01:10

NGLN