Board index » delphi » Drawing on Canvas with transparent color

Drawing on Canvas with transparent color

I have a TBitMap, which I want to draw directly onto the main
forms canvas.  The bitmap is randomly selected, but each of the
available bitmaps have a border, which I would like to be invisible.

Right now, I'm drawing using this command...

Canvas.Draw(rw,rh,Bitmap1);

rw and rh determine where, of course, and Bitmap1 holds the
bitmap which was randomly choosen.

But I haven't figured out how to make the background of the bitmap
transparent.  It draws the entire rectangle on top of what is already
there.

I'd like to draw everything except a given color - say black - so
that I can design the .BMP's using a black background, and have
the black part of the design not be drawn when I put it on the
canvas.  I think.  :^)

If it matters, the project is a screensaver, dropping different heart
.BMP's onto the screen.  I'm hoping to have the rest of this worked
out in time to give it to my sweetie for valentines...  :^)

Any help appreciated.  Ideally, send replies via Email as well as
to the newsgroup.  Thanks.

steph...@airmail.net
http://web2.airmail.net/stephenw/

 

Re:Drawing on Canvas with transparent color


Stephen,
  Try using an TImageList (they do exist in Delphi 1.0 but aren't
documented).  Something like the following should work:

Procedure TForm1.Button1Click(Sender: TObject);
var
  IL : TImageList;
begin
    {Create an ImageList with the dimensions of the bitmap.}
  IL := TImageList.Create(Image1.Picture.Bitmap.Width,
                          Image1.Picture.Bitmap.Height);
  IL.AddMasked(Image1.Picture.Bitmap, clRed);  {will mask all red pixels to
transparent}
  IL.Draw(Form1.Canvas, 0, 0, 0);                {draw to whatever canvas
you want}
         {draw on what, X, Y, which item in ImageList};
end;

When you creat your program you could load numerous different sized hearts
into the image list and then just fire them to the form's canvas from the
imagelist.  BTW make sure the bitmaps are the same size just change the
size of the heart on the bitmap.  If your using D2 the online help has some
more info on the TImageList.

Hope this helps!
--
Rod Geraghty (:>)
GERA-Tech
Ottawa, Canada
gera...@ibm.net

Stephen Whitis <steph...@airmail.net> wrote in article
<3302d3bc.23621...@news.airmail.net>...

Quote

> I have a TBitMap, which I want to draw directly onto the main
> forms canvas.  The bitmap is randomly selected, but each of the
> available bitmaps have a border, which I would like to be invisible.

> Right now, I'm drawing using this command...

> Canvas.Draw(rw,rh,Bitmap1);

> rw and rh determine where, of course, and Bitmap1 holds the
> bitmap which was randomly choosen.

> But I haven't figured out how to make the background of the bitmap
> transparent.  It draws the entire rectangle on top of what is already
> there.

> I'd like to draw everything except a given color - say black - so
> that I can design the .BMP's using a black background, and have
> the black part of the design not be drawn when I put it on the
> canvas.  I think.  :^)

> If it matters, the project is a screensaver, dropping different heart
> .BMP's onto the screen.  I'm hoping to have the rest of this worked
> out in time to give it to my sweetie for valentines...  :^)

> Any help appreciated.  Ideally, send replies via Email as well as
> to the newsgroup.  Thanks.

> steph...@airmail.net
> http://web2.airmail.net/stephenw/

Other Threads