【发布时间】:2019-02-06 18:29:39
【问题描述】:
在TForm 的OnPaint 事件中,我想绘制不覆盖背景或其他绘制对象的位图,因为它们具有透明部分。
如果我在图像上绘制图像,它会起作用。
但是当我在表单的Canvas 上绘图时,它不起作用:图像的白色部分,应该是透明的,用白色方形颜色覆盖了 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);
【问题讨论】:
-
@Remy Lebeau 有没有办法同时获得(拉伸+透明度),上面的代码只能给出其中一个。
-
@Lofti 正如我在之前的 cmets 中已经说过的(这似乎已经被删除了),使用
Images->Draw(Images->Canvas, ...)时的问题是你没有在绘制之前设置Image->Width和Image->Height在Image。Images->GetBitmap()会为您处理,但Images->Draw()不会,因此您需要将其添加到您的代码中。 -
@Remy 仍然没有位图的透明度。我该怎么做
TRect R1; 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->Transparent=true; Image->Width = 32; Image->Height = 32; //MainForm->Images->GetBitmap(ImgIndex[HisType]+Rotation, Image); MainForm->Images->Draw(Image->Canvas, 0, 0, ImgIndex[HisType]+Rotation); //Canvas->Draw(X,Y,Image); Canvas->StretchDraw(DstRect, Image); delete Image; -
其实我的图片是不透明的并且Image->Transparent=true;不会使其透明。缺少一些东西
-
尝试先绘制位图,然后设置
Transparent=true,如果需要也可以设置TransparentColor。
标签: c++ canvas c++builder tbitmap