是否有FMX函数在运行时设置TImage的透明颜色? [英] Is there an FMX function to set a TImage's transparent color at runtime?

查看:156
本文介绍了是否有FMX函数在运行时设置TImage的透明颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在FMX(FireMonkey)中的表单上有一个TImage。我想在运行时将位图加载到TImage中,其中该位图应具有由位图中第一个像素的颜色定义的透明背景。每种位图的颜色可能有所不同。

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.

我知道如何在设计时使用TImage的MultiResBitmap编辑器来做到这一点。但是,我找不到在运行时如何执行此操作的任何示例。我是否必须手动执行此操作(获取位图中第一个像素的颜色,然后遍历所有像素并将匹配的所有像素设置为透明),或者是否有更简单的方法来做到这一点?

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?

推荐答案

此函数将使用第一个像素的颜色将位图中的某种颜色设置为透明,即claNull。

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

这篇关于是否有FMX函数在运行时设置TImage的透明颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆