0

実行時に多くの画像をロードし、それぞれを Texture2D オブジェクトに割り当てて XNA を使用して表示しようとしています。TitleContainer.OpenStream("Content/"+fileName+".png") を使用します。プロジェクトを実行すると、この例外に直面します。

タイプ 'System.IO.FileNotFoundException' の未処理の例外が Microsoft.Xna.Framework.dll で発生しました追加情報: "Content\Background.png" の読み込み中にエラーが発生しました。ファイルが見つかりません。

すべての画像はコンテンツに設定されていますが。

これは、イメージをロードして Texture2D オブジェクトを作成するメソッドのすべてのコードです。

private static Texture2D LoadTextureStream(GraphicsDevice graphics, string loc) { Texture2D ファイル = null; RenderTarget2D 結果 = 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;
    }

何か助けて?? (注: Windows 7 で XNA 4.0 を使用しています)

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 に答える