2

我想检查所选矩形是否透明:

spriteBatch.Draw(texture, new Vector2(0, 0), 
           new Rectangle(0, 0, 16, 16), Color.White);

是否可以?

4

1 回答 1

2

是的,有可能。您必须检查该区域中的所有像素都是透明的。请注意,这是一个相当缓慢的操作。

这是一个应该做你想做的方法:

bool IsRegionTransparent(Texture2D texture, Rectangle r)
{
    int size = r.Width * r.Height;
    Color[] buffer = new Color[size];
    texture.GetData(0, r, buffer, 0, size);
    return buffer.All(c => c == Color.Transparent);
}

请注意,我没有编译、测试或优化上述内容。此外,它是为预乘纹理设计的(XNA 4.0 中的默认设置)。

于 2011-12-04T05:02:55.423 回答