# XNA Textures dissappear when game window is resized

[SOLVED] - See linked post in the P.S.

I have a set of Texture2Ds generated in the Load method with this function:

Texture Generation:

private Texture2D[,] createTextureSet(TileInfo[,] map)
{
Texture2D[,] texSet = new Texture2D[map.GetLength(0), map.GetLength(1)];

using (SpriteBatch sprb = new SpriteBatch(GraphicsDevice))
{
for (int row = 0; row < map.GetLength(0); row++)
{
for (int col = 0; col < map.GetLength(1); col++)
{
RenderTarget2D renderer = new RenderTarget2D(this.GraphicsDevice, 300, 300);
GraphicsDevice.SetRenderTarget(renderer);
renderer.Name = map[row, col].buildings[0].buildingType;
GraphicsDevice.Clear(Color.Transparent);
sprb.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied);
sprb.Draw(TextureFromFile(resourceDirectory + @"Images\Tile Backgrounds\" + map[row, col].tileType + @"\" + map[row, col].tileBackground + ".png"), renderer.Bounds, Color.White);
sprb.Draw(TextureFromFile(resourceDirectory + @"Images\Buildings\" + map[row, col].buildings[0].buildingType + ".png"), (Vector2)map[row, col].buildings[0].pointWithinTile, Color.White);
sprb.End();
texSet[row, col] = renderer;
}
}
}

GraphicsDevice.SetRenderTarget(null);
return texSet;
}


The tiles are drawn on the screen using the following code:

Texture Rendering

for (int tx = -1; tx < 2; tx++)
{
for (int ty = -1; ty < 2; ty++)
{
gameSpriteBatch.Draw(mapTextureSet[gameCharacter.characterTile.tileX + tx, gameCharacter.characterTile.tileY + ty], determinePositionToDrawTile(gameCharacter, map[gameCharacter.characterTile.tileX + tx, gameCharacter.characterTile.tileY + ty], tx, ty, resizeX, resizeY), null, Color.White, 0f, Vector2.Zero, new Vector2(resizeX, resizeY), SpriteEffects.None, 0f);
}
}


These textures are then drawn on the screen based on where on the map the player is. The game runs fine, until the game window is resized by the user. After that, only the character sprite remains on the screen during the redraws, and the screen remains the default Color.BlanchedAlmond background of my game. Any help is thanked in advance.

[ ! ] P.S. [ ! ]

Before marking this as a duplicate! I have looked at this and tried most of the applicable solutions from it, none of which have worked, so I assume that this error is deeper than that one.

• Why are you calling Thread.Sleep? – Andrew Russell Feb 20 '14 at 1:21
• Is that still in there? Oh, it is. This is supposed to randomly generate textures using a Random(), but it was always getting the same texture, so I added the Sleep() to see if giving it a delay would allow the Random.Next() time to change, but it didn't. I guess I just left it in there, I'll take it out. – ApachePilotMPE Feb 20 '14 at 1:28
• Are you using Random correctly? See this answer. – Andrew Russell Feb 20 '14 at 1:30
• Well, I've fixed the Random texture issue, its now just the resize/minimize texture error that's the problem. – ApachePilotMPE Feb 20 '14 at 1:30
• What does your TextureFromFile method do? Also, my SO answer that you linked does seem to exactly describe the problem you are having. Did you try implementing "Option 3" from that answer? Can you show the code you used? – Andrew Russell Feb 20 '14 at 1:34

But to finish what I was typing up, you can verify that this is the problem by adding this code right after you create your RenderTarget2D renderer:
renderer.ContentLost += (s,a) => System.Diagnostics.Debug.Assert(false);