I am trying to load many images at runtime and assign each one to Texture2D object to be displayed usnig XNA and i use TitleContainer.OpenStream("Content/"+fileName+".png").When i run the project i face this exception:
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Microsoft.Xna.Framework.dll Additional information: Error loading "Content\Background.png". File not found.
Although all images are set in Content.
and this is the all code of the method that load image and create Texture2D object.
private static Texture2D LoadTextureStream(GraphicsDevice graphics, string loc) { Texture2D file = null; RenderTarget2D result = null;
using (Stream titleStream = TitleContainer.OpenStream("Content/" + loc + ".png"))
{
file = Texture2D.FromStream(graphics, titleStream);
}
//Setup a render target to hold our final texture which will have premulitplied alpha values
result = new RenderTarget2D(graphics, file.Width, file.Height);
graphics.SetRenderTarget(result);
graphics.Clear(Color.Black);
//Multiply each color by the source alpha, and write in just the color values into the final texture
if (blendColor == null)
{
blendColor = new BlendState();
blendColor.ColorWriteChannels = ColorWriteChannels.Red | ColorWriteChannels.Green | ColorWriteChannels.Blue;
blendColor.AlphaDestinationBlend = Blend.Zero;
blendColor.ColorDestinationBlend = Blend.Zero;
blendColor.AlphaSourceBlend = Blend.SourceAlpha;
blendColor.ColorSourceBlend = Blend.SourceAlpha;
}
SpriteBatch spriteBatch = new SpriteBatch(graphics);
spriteBatch.Begin(SpriteSortMode.Immediate, blendColor);
spriteBatch.Draw(file, file.Bounds, Color.White);
spriteBatch.End();
//Now copy over the alpha values from the PNG source texture to the final one, without multiplying them
if (blendAlpha == null)
{
blendAlpha = new BlendState();
blendAlpha.ColorWriteChannels = ColorWriteChannels.Alpha;
blendAlpha.AlphaDestinationBlend = Blend.Zero;
blendAlpha.ColorDestinationBlend = Blend.Zero;
blendAlpha.AlphaSourceBlend = Blend.One;
blendAlpha.ColorSourceBlend = Blend.One;
}
spriteBatch.Begin(SpriteSortMode.Immediate, blendAlpha);
spriteBatch.Draw(file, file.Bounds, Color.White);
spriteBatch.End();
//Release the GPU back to drawing to the screen
graphics.SetRenderTarget(null);
return result as Texture2D;
}
Any help ?? (Note: iam working with XNA 4.0 on windows 7)