0

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)

4

3 回答 3

3

首先,查看您的 bin 文件夹并确保该文件在运行时确实存在。如果没有,请右键单击有问题的内容项,并确保将构建操作设置为“复制到输出目录”或“如果更新则复制”。

于 2012-07-21T13:21:31.003 回答
0

我可能错了,但我认为问题出在:

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

尝试在没有“.png”短语的情况下使用它并检查“Content\\” - 任何一个或两个都应该工作。

于 2012-07-21T13:41:15.623 回答
0

(Xbox360)对我来说,解决方案是“内容”部分的前缀。

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

然而

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

澄清:文件“explosion.wav”在解决方案资源管理器的树视图中的“声音”中可见,而不是“内容/声音”,“内容”必须反映板载运行时存储文件夹分类。

于 2014-07-30T16:56:36.473 回答