【问题标题】:C++ builder transparent bitmaps cover the canvasC++ builder 透明位图覆盖画布
【发布时间】:2019-02-06 18:29:39
【问题描述】:

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

如果我在图像上绘制图像,它会起作用。

但是当我在表单的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->WidthImage->HeightImageImages->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


【解决方案1】:

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

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

【讨论】:

  • 是的,雷米,这行得通,但我只举了一个简单的例子来问我的问题。事实上,我需要在 Form->Canvas 上绘制一些组件(当我用鼠标移动它们时)。是否有可能从 Image 而不是 TImageList 中绘制? (图像不存在绘图)
  • @Lotfi 你为什么要手动绘制组件?你应该让他们正常地画自己。你到底想完成什么?
  • 我正在将我在 Pascal Object 7.0 上制作的旧程序转换为 C++ builder baghli.com/powerd.html,因此需要绘制组件集合。您的方法适用于普通图像,但其中一个需要拉伸,然后才能在表单上绘制。所以我需要能够从具有透明度的位图或 Timage 中进行绘制。
  • @Lotfi 在位图上创建透明的TBitmapTImageList.Draw(),然后在位图上创建Canvas.StretchDraw()
【解决方案2】:

找到解决方案,感谢 Remy。我们必须首先用一种颜色填充新创建的位图,并且不要让它为空,以使透明度起作用...

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;

【讨论】:

  • 你应该为FillRect()定义一个特定的颜色(默认为白色):Image->Canvas->Brush->Color = ...;(常用clFuschia)。如果您没有设置TransparentColor,位图将使用最左边第一个像素的颜色。如果Draw() 发生 在该像素中绘制,则您的位图可能不会按照您期望的方式呈现。所以你应该明确地将TransparentColor 设置为你告诉FillRect() 使用的颜色。
  • 是的,我明白,但没关系,因为所有绘图都远离边缘
  • 不仅仅是边缘,还有颜色。就像我说的,默认的画笔颜色是白色的,所以如果你的绘图包含白色像素,它们最终会是透明的,这可能是你想要的,也可能不是你想要的,这取决于你的特定图像。只是说...
猜你喜欢
  • 2014-03-13
  • 2013-07-21
  • 1970-01-01
  • 2018-03-19
  • 1970-01-01
  • 1970-01-01
  • 2016-12-10
  • 2017-01-18
  • 1970-01-01
相关资源
最近更新 更多