Board index » delphi » How to merge two bmp images and save it into one

How to merge two bmp images and save it into one


2004-09-08 08:00:43 AM
delphi40
Hello friends:
Please help. can some body provide small sample code
on how to merge two images bmp then save it into one
single bmp file.
for example: I have two images of a check front and back
I want to merge these two images into one single bmp
under a sinlgle file bmp.
Thank you very much in advance
Marcos
 
 

Re:How to merge two bmp images and save it into one

Marcos writes:
Quote
Hello friends:

Please help. can some body provide small sample code
on how to merge two images bmp then save it into one
single bmp file.

for example: I have two images of a check front and back
I want to merge these two images into one single bmp
under a sinlgle file bmp.

Thank you very much in advance

Marcos


The CopyRect function is what you need.
Assume that you have two bitmaps, Chk1 and Chk2, that hold the images
that you have loaded. You have an output bitmap, Both, that holds each
image. Then:
// stack two images top to bottom in a single bitmap
var
Chk1, Chk2, Both : TBitmap
begin
<load up the front and back check images into their bitmaps>
Both := TBitmap.Create;
If Chk1.Width>Chk2.Width then
Both.Width := Chk1.Width
else
Both.Width := Chk2.Width;
Both.Height := Chk1.Height + Chk1.Height;
Both.Canvas.CopyRect(Rect(0,0,Chk1.Width,Chk1.Height),
Chk1.Picture.Bitmap.Canvas,Rect(0,0,Chk1.Width,Chk1.Height));
Both.Canvas.CopyRect(Rect(0,Chk1.Height,Chk2.Width,Chk2.Height),
Chk2.Picture.Bitmap.Canvas,Rect(0,0,Chk2.Width,Chk2.Height));
Both.SaveToFile('s:\temp\wowza.bmp');
Both.Free.Free;
 

Re:How to merge two bmp images and save it into one

Mr.Wayne Herbert,
Thank you very much for your fast response, I try the
code you provided and it works GREAT!!!!
Once again thank you sir, I appreciated your help
Marcos
"Wayne Herbert" <XXXX@XXXXX.COM>writes
Quote
Marcos writes:

>Hello friends:
>
>Please help. can some body provide small sample code
>on how to merge two images bmp then save it into one
>single bmp file.
>
>for example: I have two images of a check front and back
>I want to merge these two images into one single bmp
>under a sinlgle file bmp.
>
>Thank you very much in advance
>
>Marcos
>
>

The CopyRect function is what you need.

Assume that you have two bitmaps, Chk1 and Chk2, that hold the images
that you have loaded. You have an output bitmap, Both, that holds each
image. Then:

// stack two images top to bottom in a single bitmap
var
Chk1, Chk2, Both : TBitmap
begin
<load up the front and back check images into their bitmaps>
Both := TBitmap.Create;
If Chk1.Width>Chk2.Width then
Both.Width := Chk1.Width
else
Both.Width := Chk2.Width;
Both.Height := Chk1.Height + Chk1.Height;

Both.Canvas.CopyRect(Rect(0,0,Chk1.Width,Chk1.Height),
Chk1.Picture.Bitmap.Canvas,Rect(0,0,Chk1.Width,Chk1.Height));
Both.Canvas.CopyRect(Rect(0,Chk1.Height,Chk2.Width,Chk2.Height),
Chk2.Picture.Bitmap.Canvas,Rect(0,0,Chk2.Width,Chk2.Height));

Both.SaveToFile('s:\temp\wowza.bmp');
Both.Free.Free;