问题
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