简体   繁体   English

C# XNA 4.0 异常:“无法打开文件”

[英]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.我在 XNA 4.0 Game Studio (C#) 中工作,我正在尝试使用 LoadContent() 方法加载图像。 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.我已经在这个游戏中加载了许多图像文件,它们都可以 100% 正常工作,但由于某种原因,XNA 不会在我的 loadContent 方法之一中打开文件。 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."它找到该文件,但在调用 LoadContent() 方法并显示“无法打开文件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.只是在这里,在这个 class 中,在这个 loadContent 方法中,它们将不起作用。

The Content.Load methods does not load files, it loads specialized content or assets. Content.Load 方法不加载文件,它加载专门的内容或资产。 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.因为 XNA 是独立于平台的,在一台机器上你可能使用更大的图像或不同的图像,你只需要改变管道中的资产,就可以重用代码。

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:只是为了添加dowhilefor 的出色答案,如果您想加载原始.jpg文件(或.png ),您可以这样做:

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.使用ContentManager加载某些内容不同,在这种情况下“拥有”它。 This means you are responsible for calling Dispose() on it in UnloadContent .这意味着您有责任在UnloadContent中调用Dispose()

Also unlike when you go through the content pipeline (using the default settings), the texture that you load will not have premultiplied-alpha.您通过内容管道(使用默认设置)时 go 不同,您加载的纹理不会具有预乘 alpha。 You need to apply premultiplication yourself, or render it with BlendState.NonPremultiplied .您需要自己应用预乘,或使用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 .当然,除非出于某种原因(例如:您正在从 Internet 下载图像,或者您让最终用户选择它们),否则您应该使用内容管道

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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