简体   繁体   中英

C# XNA 4.0 Exception: “Cannot Open File”

I am working in XNA 4.0 Game Studio (C#), and I am trying to load an image using the LoadContent() method. I have loaded numerous image files into this game and they all work 100% fine, but for some reason, XNA will not open files inside one of my loadContent methods. Here is the method:

    protected override void LoadContent()
    {
        //spriteBatch = new SpriteBatch(GraphicsDevice);
        //Sets up an array of textures to be used in the Icon class
        Texture2D[] icons = new Texture2D[24];

        #region Loading talent textures
        //These are all of the icons that need to be loaded for the talents
        //Paladin
        icons[0] = Content.Load<Texture2D>(@"C:\Users\Student\Desktop\Dropbox\Public\platformer\Platformer\Content\Talents\blade_of_light3.jpg");
        icons[1] = Content.Load<Texture2D>("Talents\\divine_grace");
        icons[2] = Content.Load<Texture2D>("Talents\\divine_storm");
        icons[3] = Content.Load<Texture2D>("Talents\\hammer_of_the_righteous");
        icons[4] = Content.Load<Texture2D>("Talents\\healing_hands");
        icons[5] = Content.Load<Texture2D>("Talents\\heavenly_fury");
        icons[6] = Content.Load<Texture2D>("Talents/momentum_of_light");
        icons[7] = Content.Load<Texture2D>("Talents/retribution");
        icons[8] = Content.Load<Texture2D>("Talents/righteous_fury");
        icons[9] = Content.Load<Texture2D>("Talents/sanctuary");
        icons[10] = Content.Load<Texture2D>("Talent/searing_light");
        icons[11] = Content.Load<Texture2D>("Talent/wrath_of_the_heavens");

        //Warrior
        icons[12] = Content.Load<Texture2D>(@"Talents\bloodstorm");
        icons[13] = Content.Load<Texture2D>(@"Talents\bloodthirst");
        icons[14] = Content.Load<Texture2D>(@"Talents\die_by_the_sword");
        icons[15] = Content.Load<Texture2D>(@"Talents\furious_blades");
        icons[16] = Content.Load<Texture2D>(@"Talents\unleash_rage");
        icons[17] = Content.Load<Texture2D>(@"Talents\lifeblood");
        icons[18] = Content.Load<Texture2D>(@"Talents\red_like_my_rage");
        icons[19] = Content.Load<Texture2D>(@"Talents\eternal_thirst");
        icons[20] = Content.Load<Texture2D>(@"Talents\bladesurge");
        icons[21] = Content.Load<Texture2D>(@"Talents\bathed_in_blood");
        icons[22] = Content.Load<Texture2D>(@"Talents\bladerunner");
        icons[23] = Content.Load<Texture2D>(@"Talents\bloodfury");
        icons[24] = Content.Load<Texture2D>(@"Talents\grapple_chain");
        #endregion

As you can see, I have tried using the ENTIRE file location. It finds the file, but throws an exception when the LoadContent() method is called and says "Cannot open file blade_of_light3."

I do not get any errors about escape paths or anything like that, and I have used this sort of file path for other images, and they work fine. It's just here, in this class, in this loadContent method, that they won't work.

The Content.Load methods does not load files, it loads specialized content or assets. Have a look at this . You can't load files directly, you can only load assets. These assets are generated via the content pipeline . This is mainly to provide an abstract layer for content. Because XNA is platform independent, and on one machine you may be use a bigger image or different image, you only need to change the asset in the pipeline and can reuse the code.

Just to add to dowhilefor's excellent answer , if you want to load a raw .jpg file (or .png ), you can do so like this:

using(var s = File.OpenRead(fileName))
{
    Texture2D texture = Texture2D.FromStream(GraphicsDevice, s);
}

Unlike when you load something using ContentManager , you "own" it in this case. This means you are responsible for calling Dispose() on it in UnloadContent .

Also unlike when you go through the content pipeline (using the default settings), the texture that you load will not have premultiplied-alpha. You need to apply premultiplication yourself, or render it with BlendState.NonPremultiplied .

Of course, unless you are unable to for some reason (eg: you're downloading images from the internet, or you're letting your end user pick them), you should use the content pipeline .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM