Mega Code Archive

 
Categories / Delphi / Graphic
 

How to draw a transparent bitmap

Title: How to draw a transparent bitmap Question: Answer: procedure DrawTransparentBmp(Cnv: TCanvas; x,y: Integer; Bmp: TBitmap; clTransparent: TColor); var bmpXOR, bmpAND, bmpINVAND, bmpTarget: TBitmap; oldcol: Longint; begin try bmpAND := TBitmap.Create; bmpAND.Width := Bmp.Width; bmpAND.Height := Bmp.Height; bmpAND.Monochrome := True; oldcol := SetBkColor(Bmp.Canvas.Handle, ColorToRGB(clTransparent)); BitBlt(bmpAND.Canvas.Handle, 0,0,Bmp.Width,Bmp.Height, Bmp.Canvas.Handle, 0,0, SRCCOPY); SetBkColor(Bmp.Canvas.Handle, oldcol); bmpINVAND := TBitmap.Create; bmpINVAND.Width := Bmp.Width; bmpINVAND.Height := Bmp.Height; bmpINVAND.Monochrome := True; BitBlt(bmpINVAND.Canvas.Handle, 0,0,Bmp.Width,Bmp.Height, bmpAND.Canvas.Handle, 0,0, NOTSRCCOPY); bmpXOR := TBitmap.Create; bmpXOR.Width := Bmp.Width; bmpXOR.Height := Bmp.Height; BitBlt(bmpXOR.Canvas.Handle, 0,0,Bmp.Width,Bmp.Height, Bmp.Canvas.Handle, 0,0, SRCCOPY); BitBlt(bmpXOR.Canvas.Handle, 0,0,Bmp.Width,Bmp.Height, bmpINVAND.Canvas.Handle, 0,0, SRCAND); bmpTarget := TBitmap.Create; bmpTarget.Width := Bmp.Width; bmpTarget.Height := Bmp.Height; BitBlt(bmpTarget.Canvas.Handle, 0,0,Bmp.Width,Bmp.Height, Cnv.Handle, x,y, SRCCOPY); BitBlt(bmpTarget.Canvas.Handle, 0,0,Bmp.Width,Bmp.Height, bmpAND.Canvas.Handle, 0,0, SRCAND); BitBlt(bmpTarget.Canvas.Handle, 0,0,Bmp.Width,Bmp.Height, bmpXOR.Canvas.Handle, 0,0, SRCINVERT); BitBlt(Cnv.Handle, x,y,Bmp.Width,Bmp.Height, bmpTarget.Canvas.Handle, 0,0, SRCCOPY); finally bmpXOR.Free; bmpAND.Free; bmpINVAND.Free; bmpTarget.Free; end; end;