C++生成器透明位图覆盖画布

C++ builder transparent bitmaps cover the canvas

本文关键字:覆盖 位图 透明 C++      更新时间:2023-10-16

TFormOnPaint事件中,我想绘制不覆盖背景或其他绘制对象的位图,因为它们具有透明的部分。

如果我在图像上绘制图像,它可以工作。

但是当我在表单的Canvas上绘制时,它不起作用:图像的白色部分应该是透明的,用白色正方形覆盖画布的其他对象。

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);

更新

当我使用MainForm->Images->Draw(Image->Canvas...)在图像上绘制时,我得到一个透明的正方形,里面什么都没有,我可以在其他组件上移动。

当我使用 MainForm->Images->GetBitmap(ImgIndex[HisType]+Rotation, Image) 绘制时,我在表单上获得了正确的条纹图像,但没有透明度,即它的白色部分覆盖了其他组件。

MainForm->Images->Draw(Canvas, X, Y, ImgIndex[HisType]+Rotation, dsTransparent, itImage);完成这项工作时,我需要根据 Size 变量为此组件划线。

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);

请改用Images->Draw(),让TImageList为您处理绘图:

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

解决方案,感谢雷米。我们必须首先用一种颜色填充新创建的位图,不要让它为空,以便透明度工作......

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;