{"id":147,"date":"2007-05-12T16:11:54","date_gmt":"2007-05-12T14:11:54","guid":{"rendered":" http:\/\/www.nsonic.de\/blog\/2007\/05\/drawtransparent-bitmaps\/"},"modified":"2007-05-12T16:11:56","modified_gmt":"2007-05-12T14:11:56","slug":"drawtransparent-bitmaps","status":"publish","type":"post","link":"https:\/\/www.nsonic.de\/blog\/2007\/05\/drawtransparent-bitmaps\/","title":{"rendered":"DrawTransparent Bitmaps"},"content":{"rendered":"<p><em>>Sam King wrote:<br \/>\n><br \/>\n>> I want overlay bitmap2 over bitmap1<br \/>\n>> The bitmap2 should be on top of bitmap1<br \/>\n>> i.e. the end result should be that bitmap2 should also show bits of<br \/>\n>> bitmap1.<br \/>\n>> eg a grid pattern over a map. What is the best way in delphi to handle<br \/>\n>> this.<br \/>\n<\/em><br \/>\nThere are two methods. One works using a timageList the other without. So<br \/>\nthe latter will work with other file types besides bmps. Here&#8217;s the code<br \/>\nfor both, it&#8217;s pretty clear how both work. The key thing is you must<br \/>\nspecify a transparent color:<br \/>\n<!--more--><br \/>\n[code lang=&#8221;delphi&#8221;]procedure DrawTrans(DestCanvas: TCanvas; X,Y: smallint; SrcBitmap:<br \/>\nTBitmap; AColor: TColor);<br \/>\nvar ANDBitmap, ORBitmap: TBitmap;<br \/>\nCM: TCopyMode;<br \/>\nSrc: TRect;<br \/>\nbegin<br \/>\nANDBitmap:= NIL;<br \/>\nORBitmap:= NIL;<br \/>\ntry<br \/>\nANDBitmap:= TBitmap.Create;<br \/>\nORBitmap:= TBitmap.Create;<br \/>\nSrc := Bounds(0,0, SrcBitmap.Width, SrcBitmap.Height);<br \/>\nwith ORBitmap do begin<br \/>\nWidth:= SrcBitmap.Width;<br \/>\nHeight:= SrcBitmap.Height;<br \/>\nCanvas.Brush.Color := clBlack;<br \/>\nCanvas.CopyMode := cmSrcCopy;<br \/>\nCanvas.BrushCopy(Src, SrcBitmap, Src, AColor);<br \/>\nend;<br \/>\nwith ANDBitmap do begin<br \/>\nWidth:= SrcBitmap.Width;<br \/>\nHeight:= SrcBitmap.Height;<br \/>\nCanvas.Brush.Color := clWhite;<br \/>\nCanvas.CopyMode := cmSrcInvert;<br \/>\nCanvas.BrushCopy(Src, SrcBitmap, Src, AColor);<br \/>\nend;<br \/>\nwith DestCanvas do begin<br \/>\nCM := CopyMode;<br \/>\nCopyMode := cmSrcAnd;<br \/>\nDraw(X,Y, ANDBitmap);<br \/>\nCopyMode := cmSrcPaint;<br \/>\nDraw(X,Y, ORBitmap);<br \/>\nCopyMode := CM;<br \/>\nend;<br \/>\nfinally<br \/>\nANDBitmap.Free;<br \/>\nORBitmap.Free;<br \/>\nend;<br \/>\nend;<br \/>\n[\/code]<\/p>\n<p><b>The other way:<br \/>\n<\/b><br \/>\n[code lang=&#8221;delphi&#8221;]abitmap:= tbitmap.Create;<br \/>\nimagelist1.Clear;<br \/>\nabitmap.LoadFromFile(&#8216;1.bmp&#8217;);<br \/>\nimagelist1.AddMasked(abitmap, clNone);<br \/>\nabitmap.Empty;<br \/>\nabitmap.LoadFromFile(&#8216;e1.bmp&#8217;);<br \/>\nimagelist1.AddMasked(abitmap, clWhite);<br \/>\nabitmap.Empty;<br \/>\nabitmap.LoadFromFile(&#8216;h1.bmp&#8217;);<br \/>\nimagelist1.AddMasked(abitmap, clWhite);<br \/>\nimagelist1.Draw(image1.Canvas, 0, 0, 0);<br \/>\nimagelist1.Draw(image1.Canvas, 0, 0, 1);<br \/>\nimagelist1.Draw(image1.Canvas, 0, 0, 2);<br \/>\nabitmap.free;<br \/>\n[\/code]<\/p>\n<p>[tags]Delphi, Graphic[\/tags]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>>Sam King wrote: > >> I want overlay bitmap2 over bitmap1 >> The bitmap2 should be on top of bitmap1 >> i.e. the&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[11],"tags":[75,101],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p71Tml-2n","_links":{"self":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts\/147"}],"collection":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/comments?post=147"}],"version-history":[{"count":0,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts\/147\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/tags?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}