简体   繁体   English

是否有FMX函数在运行时设置TImage的透明颜色?

[英]Is there an FMX function to set a TImage's transparent color at runtime?

I have a TImage on a form in FMX (FireMonkey). 我在FMX(FireMonkey)中的表单上有一个TImage。 I want to load a bitmap into the TImage at runtime, where the bitmap should have a transparent background defined by the first pixel's color in the bitmap. 我想在运行时将位图加载到TImage中,其中位图应具有由位图中第一个像素的颜色定义的透明背景。 This color might be different from bitmap to bitmap. 每种位图的颜色可能不同。

I know how to do this at design time, by using the MultiResBitmap editor for a TImage. 我知道在设计时如何通过对TImage使用MultiResBitmap编辑器来做到这一点。 However, I can't find any examples of how to do this at runtime. 但是,我找不到在运行时如何执行此操作的任何示例。 Do I have to do this manually (get the color of the first pixel in the bitmap, then iterate through all the pixels and set any that match to transparent), or is there a more simple way to do this? 我是否必须手动执行此操作(获取位图中第一个像素的颜色,然后遍历所有像素并将匹配的像素设置为透明),或者是否有更简单的方法来执行此操作?

This function will set a certain color in a bitmap to transparent, ie, to claNull, using the first pixel's color. 此函数将使用第一个像素的颜色将位图中的特定颜色设置为透明,即claNull。

void SetTransparent (Graphics::TBitmap *oBmp)
{
  TBitmapData bmpData;
  oBmp->Map (TMapAccess::maReadWrite, bmpData);

  TAlphaColor colorToMakeTransparent = bmpData.GetPixel (0, 0);
  TAlphaColor transparentColor = claNull;

  for (int x=0; x<bmpData.Width; x++)
  {
    for (int y=0; y<bmpData.Height; y++)
    {
      TAlphaColor color = bmpData.GetPixel (x, y);
      if (color == colorToMakeTransparent)
        bmpData.SetPixel (x, y, transparentColor);
    }
  }

  oBmp->Unmap (bmpData);
}
//---------------------------------------------------------------------------

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM