我正在为Windows Phone XNA开发,并且希望加载较小尺寸的纹理以减少不需要完整图像时的内存影响。加载纹理在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()将抛出一个处置的异常。
加载纹理的任何其他方式调整大小以减少内存使用量?