我正在为 Windows Phone XNA 开发并希望加载尺寸较小的纹理以减少不需要完整图像的内存影响。
我目前的解决方案是使用一个渲染目标来绘制并返回该渲染目标作为一个较小的纹理来使用:
public static Texture2D LoadResized(string texturePath, float scale)
{
    Texture2D texLoaded = Content.Load<Texture2D>(texturePath);
    Vector2 resizedSize = new Vector2(texLoaded.Width * scale, texLoaded.Height * scale);
    Texture2D resized = ResizeTexture(texLoaded, resizedSize);
    //texLoaded.Dispose();
    return resized;
}
public static Texture2D ResizeTexture(Texture2D toResize, Vector2 targetSize)
{
    RenderTarget2D renderTarget = new RenderTarget2D(
        GraphicsDevice, (int)targetSize.X, (int)targetSize.Y);
    Rectangle destinationRectangle = new Rectangle(
        0, 0, (int)targetSize.X, (int)targetSize.Y);
    GraphicsDevice.SetRenderTarget(renderTarget);
    GraphicsDevice.Clear(Color.Transparent);
    SpriteBatch.Begin();
    SpriteBatch.Draw(toResize, destinationRectangle, Color.White);
    SpriteBatch.End();
    GraphicsDevice.SetRenderTarget(null);
    return renderTarget;
}
这样做的原因是纹理被调整了大小,但从内存使用情况来看,纹理“texLoaded”似乎没有被释放。当使用未注释的 Dispose 方法时,SpriteBatch.End() 将抛出一个已处理的异常。
任何其他方式来加载调整大小以减少内存使用的纹理?