C++ builder transparent bitmaps cover the canvas

2k views Asked by At

In the OnPaint event of a TForm, I want to paint bitmaps that do not cover the background or other painted object because they have transparent parts.

If I draw an image over an image, it works.

But when I draw on the Form's Canvas, it does not work: the white part of the image, which is supposed to be transparent covers the other objects of the Canvas by a white square color.

Canvas->CopyMode = cmMergePaint ;
Graphics::TBitmap * Image=new Graphics::TBitmap();
Image->Transparent = true;
MainForm->Images->GetBitmap(14, Image);

Canvas->Draw(10,10,Image;
MainForm->Images->GetBitmap(0, Image);
Canvas->Draw(15,15,Image);

Update

When I draw on the Image using MainForm->Images->Draw(Image->Canvas...), I get a transparent square with nothing inside, that I can move over the other components.

When I draw using MainForm->Images->GetBitmap(ImgIndex[HisType]+Rotation, Image), I get the correct streched image on the Form, but without transparencies i.e. its white parts cover the other components.

While the MainForm->Images->Draw(Canvas, X, Y, ImgIndex[HisType]+Rotation, dsTransparent, itImage); does the job, I need to strech it for this component depending on a Size variable.

TRect DstRect(X,Y, X+32 + ( 1 - Rotation ) * 32 * Size, Y+32 + Rotation * 32 * Size);
Graphics::TBitmap * Image=new Graphics::TBitmap();
Image->Transparent=true;
//MainForm->Images->GetBitmap(ImgIndex[HisType]+Rotation, Image);
MainForm->Images->Draw(Image->Canvas, 0, 0, ImgIndex[HisType]+Rotation, dsTransparent, itImage);
Canvas->StretchDraw(DstRect, Image);
delete Image;

//MainForm->Images->Draw(Canvas, X, Y, ImgIndex[HisType]+Rotation, dsTransparent, itImage);
2

There are 2 answers

3
Lotfi On BEST ANSWER

Found the solution, Thank to Remy. We must first fill the newly created bitmap with one color and not let it empty in order to transparency to work...

Size=1; //debug
TRect DstRect(X,Y, X+32 + ( 1 - Rotation ) * 32 * Size, Y+32 + Rotation * 32 * Size);
Graphics::TBitmap * Image=new Graphics::TBitmap();
Image->Width = 32;
Image->Height = 32;
Image->Canvas->FillRect(Rect(0,0,32,32));
MainForm->Images->GetBitmap(ImgIndex[HisType]+Rotation, Image);
//MainForm->Images->Draw(Image->Canvas, 0, 0, ImgIndex[HisType]+Rotation, dsTransparent, itImage);
Image->Canvas->Pen->Color = clRed;
Image->Canvas->MoveTo( 3, 3 );
Image->Canvas->LineTo( 29, 29 );
Image->Transparent=true;
Canvas->StretchDraw(DstRect, Image);
delete Image;
4
Remy Lebeau On

Use Images->Draw() instead, let the TImageList handle the drawing for you:

MainForm->Images->Draw(Canvas, 10, 10, 14, dsTransparent, itImage);
MainForm->Images->Draw(Canvas, 15, 15, 0, dsTransparent, itImage);