0
votes

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)

3

3 Answers

3
votes

First, look in your bin folder and ensure that the file actually exists at run time. If not, right click on your content item in question and ensure that that the build action is set to "copy to output directory" or "copy if newer".

0
votes

I may be wrong but I think the problem is here:

using (Stream titleStream = TitleContainer.OpenStream("Content/" + loc + ".png"))

Try using this without ".png" phrase and check "Content\\" - any or both of this should work.

0
votes

(Xbox360) for me the solution was prefixing the "Content" part.

   Stream soundfile = TitleContainer.OpenStream(@"Sound/explosion.wav"); //<- FAIL
   boomEffect = SoundEffect.FromStream(soundfile);
   soundEffectInstance = boomEffect.CreateInstance();

however

   Stream soundfile = TitleContainer.OpenStream(@"Content/Sound/explosion.wav"); //<- Win
   boomEffect = SoundEffect.FromStream(soundfile);
   soundEffectInstance = boomEffect.CreateInstance();

For clarification: The file "explosion.wav" is visibly in "Sound" in the tree view of solution explorer, not "Content/Sound", the "Content" must reflect the onboard runtime storage folder taxonomy.