Mastodon
Programmierung

DrawTransparent Bitmaps

>Sam King wrote:
>
>> I want overlay bitmap2 over bitmap1
>> The bitmap2 should be on top of bitmap1
>> i.e. the end result should be that bitmap2 should also show bits of
>> bitmap1.
>> eg a grid pattern over a map. What is the best way in delphi to handle
>> this.

There are two methods. One works using a timageList the other without. So
the latter will work with other file types besides bmps. Here’s the code
for both, it’s pretty clear how both work. The key thing is you must
specify a transparent color:

[code lang=”delphi”]procedure DrawTrans(DestCanvas: TCanvas; X,Y: smallint; SrcBitmap:
TBitmap; AColor: TColor);
var ANDBitmap, ORBitmap: TBitmap;
CM: TCopyMode;
Src: TRect;
begin
ANDBitmap:= NIL;
ORBitmap:= NIL;
try
ANDBitmap:= TBitmap.Create;
ORBitmap:= TBitmap.Create;
Src := Bounds(0,0, SrcBitmap.Width, SrcBitmap.Height);
with ORBitmap do begin
Width:= SrcBitmap.Width;
Height:= SrcBitmap.Height;
Canvas.Brush.Color := clBlack;
Canvas.CopyMode := cmSrcCopy;
Canvas.BrushCopy(Src, SrcBitmap, Src, AColor);
end;
with ANDBitmap do begin
Width:= SrcBitmap.Width;
Height:= SrcBitmap.Height;
Canvas.Brush.Color := clWhite;
Canvas.CopyMode := cmSrcInvert;
Canvas.BrushCopy(Src, SrcBitmap, Src, AColor);
end;
with DestCanvas do begin
CM := CopyMode;
CopyMode := cmSrcAnd;
Draw(X,Y, ANDBitmap);
CopyMode := cmSrcPaint;
Draw(X,Y, ORBitmap);
CopyMode := CM;
end;
finally
ANDBitmap.Free;
ORBitmap.Free;
end;
end;
[/code]

The other way:

[code lang=”delphi”]abitmap:= tbitmap.Create;
imagelist1.Clear;
abitmap.LoadFromFile(‘1.bmp’);
imagelist1.AddMasked(abitmap, clNone);
abitmap.Empty;
abitmap.LoadFromFile(‘e1.bmp’);
imagelist1.AddMasked(abitmap, clWhite);
abitmap.Empty;
abitmap.LoadFromFile(‘h1.bmp’);
imagelist1.AddMasked(abitmap, clWhite);
imagelist1.Draw(image1.Canvas, 0, 0, 0);
imagelist1.Draw(image1.Canvas, 0, 0, 1);
imagelist1.Draw(image1.Canvas, 0, 0, 2);
abitmap.free;
[/code]

[tags]Delphi, Graphic[/tags]

0 Kommentare zu “DrawTransparent Bitmaps

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.