【发布时间】:2013-02-06 12:02:04
【问题描述】:
我正在为 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() 将抛出一个已处理的异常。
任何其他方式来加载调整大小以减少内存使用的纹理?
【问题讨论】:
标签: windows-phone-7 memory xna texture2d rendertarget