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

笑着哭i 提交于 2020-01-17 10:57:57

问题


I have a TImage on a form in FMX (FireMonkey). 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. 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. 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?


回答1:


This function will set a certain color in a bitmap to transparent, i.e., to claNull, using the first pixel's color.

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


来源:https://stackoverflow.com/questions/22948442/is-there-an-fmx-function-to-set-a-timages-transparent-color-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!