The following
unit draws two bitmaps
on a form. One
is used
as the background,
and the second one
as the foreground. The
foreground bitmap
is displayed
as a "transparent" bitmap.
Read the comments
for a complete (well sort
of) explanation.
{ Purpose: Display a transparent bitmap loaded from a file
Author: Michael Vincze (vincze@ti.com)
Date: 04/20/95
Usage: Create a blank form, named Form1, compile and run.
Limits: This unit has been tested for both 16 and 256 color bitmaps.
It is assumed that the lower left pixel of the bitmap
represents the transparent color.
Notes: If this file is to be used for any purpose please leave
this header intact and give credit to the author if used for
any purpose.
Please contact the author if any improvements are made.
The author stakes no claim for this programs usefullness
or purpose.
Version: 1.00 04/20/95 Initial creation
}
unit Tbmpu;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, ExtCtrls;
type
TForm1 =
class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
private
{ Private declarations }
ImageForeGround: TImage;
ImageBackGround: TImage;
public
{ Public declarations }
end;
procedure DrawTransparentBitmap (ahdc: HDC;
Image: TImage;
xStart, yStart: Word);
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure DrawTransparentBitmap (ahdc: HDC;
Image: TImage;
xStart, yStart: Word);
var
TransparentColor: TColor;
cColor : TColorRef;
bmAndBack,
bmAndObject,
bmAndMem,
bmSave,
bmBackOld,
bmObjectOld,
bmMemOld,
bmSaveOld : HBitmap;
hdcMem,
hdcBack,
hdcObject,
hdcTemp,
hdcSave : HDC;
ptSize : TPoint;
begin
{ set the transparent color to be the lower left pixel of the bitmap
}
TransparentColor := Image.Picture.Bitmap.Canvas.Pixels[0,
Image.Height - 1];
TransparentColor := TransparentColor
or $02000000;
hdcTemp := CreateCompatibleDC (ahdc);
SelectObject (hdcTemp, Image.Picture.Bitmap.Handle);
{ select the
bitmap }
{ convert bitmap dimensions from device to logical points
}
ptSize.x := Image.Width;
ptSize.y := Image.Height;
DPtoLP (hdcTemp, ptSize, 1);
{ convert from device logical points }
{ create some DCs to hold temporary data
}
hdcBack := CreateCompatibleDC(ahdc);
hdcObject := CreateCompatibleDC(ahdc);
hdcMem := CreateCompatibleDC(ahdc);
hdcSave := CreateCompatibleDC(ahdc);
{ create a bitmap for each DC
}
{ monochrome DC
}
bmAndBack := CreateBitmap (ptSize.x, ptSize.y, 1, 1,
nil);
bmAndObject := CreateBitmap (ptSize.x, ptSize.y, 1, 1,
nil);
bmAndMem := CreateCompatibleBitmap (ahdc, ptSize.x, ptSize.y);
bmSave := CreateCompatibleBitmap (ahdc, ptSize.x, ptSize.y);
{ each DC must select a bitmap object to store pixel data
}
bmBackOld := SelectObject (hdcBack, bmAndBack);
bmObjectOld := SelectObject (hdcObject, bmAndObject);
bmMemOld := SelectObject (hdcMem, bmAndMem);
bmSaveOld := SelectObject (hdcSave, bmSave);
{ set proper mapping mode
}
SetMapMode (hdcTemp, GetMapMode (ahdc));
{ save the bitmap sent here, because it will be overwritten
}
BitBlt (hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);
{ set the background color of the source DC to the color.
contained in the parts of the bitmap that should be transparent
}
cColor := SetBkColor (hdcTemp, TransparentColor);
{ create the object mask for the bitmap by performing a BitBlt()
from the source bitmap to a monochrome bitmap
}
BitBlt (hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);
{ set the background color of the source DC back to the original color
}
SetBkColor (hdcTemp, cColor);
{ create the inverse of the object mask
}
BitBlt (hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0,
NOTSRCCOPY);
{ copy the background of the main DC to the destination
}
BitBlt (hdcMem, 0, 0, ptSize.x, ptSize.y, ahdc, xStart, yStart,
SRCCOPY);
{ mask out the places where the bitmap will be placed
}
BitBlt (hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);
{ mask out the transparent colored pixels on the bitmap
}
BitBlt (hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);
{ XOR the bitmap with the background on the destination DC
}
BitBlt (hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);
{ copy the destination to the screen
}
BitBlt (ahdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0,
SRCCOPY);
{ place the original bitmap back into the bitmap sent here
}
BitBlt (hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);
{ delete the memory bitmaps
}
DeleteObject (SelectObject (hdcBack, bmBackOld));
DeleteObject (SelectObject (hdcObject, bmObjectOld));
DeleteObject (SelectObject (hdcMem, bmMemOld));
DeleteObject (SelectObject (hdcSave, bmSaveOld));
{ delete the memory DCs
}
DeleteDC (hdcMem);
DeleteDC (hdcBack);
DeleteDC (hdcObject);
DeleteDC (hdcSave);
DeleteDC (hdcTemp);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
{ create image controls for two bitmaps and set their parents
}
ImageForeGround := TImage.Create (Form1);
ImageForeGround.Parent := Form1;
ImageBackGround := TImage.Create (Form1);
ImageBackGround.Parent := Form1;
{ load images
}
ImageBackGround.Picture.LoadFromFile
('
c:\delphi\images\splash\16color\earth.bmp');
ImageForeGround.Picture.LoadFromFile
('
c:\delphi\images\splash\16color\athena.bmp');
{ set background image size to its bitmap dimensions
}
with ImageBackGround
do
begin
Left := 0;
Top := 0;
Width := Picture.Width;
Height := Picture.Height;
end;
{ set the foreground image size centered in the background image
}
with ImageForeGround
do
begin
Left := (ImageBackGround.Picture.Width - Picture.Width)
div 2;
Top := (ImageBackGround.Picture.Height - Picture.Height)
div 2;
Width := Picture.Width;
Height := Picture.Height;
end;
{ do not show the transparent bitmap as it will be displayed
(BitBlt()ed)
by the DrawTransparentBitmap() function
}
ImageForeGround.Visible := False;
{ draw the tranparent bitmap
note how the DC of the foreground is used in the function below
}
DrawTransparentBitmap (ImageBackGround.Picture.Bitmap.Canvas.Handle,
{HDC}
ImageForeGround,
{TImage}
ImageForeGround.Left,
{X}
ImageForeGround.Top
{Y} );
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
{ free images
}
ImageForeGround.Free;
ImageBackGround.Free;
end;