[英]C# XNA 4.0 Exception: “Cannot Open File”
我在 XNA 4.0 Game Studio (C#) 中工作,我正在嘗試使用 LoadContent() 方法加載圖像。 我已經在這個游戲中加載了許多圖像文件,它們都可以 100% 正常工作,但由於某種原因,XNA 不會在我的 loadContent 方法之一中打開文件。 這是方法:
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
如您所見,我嘗試使用整個文件位置。 它找到該文件,但在調用 LoadContent() 方法並顯示“無法打開文件blade_of_light3”時引發異常。
我沒有收到有關轉義路徑或類似內容的任何錯誤,並且我已將這種文件路徑用於其他圖像,並且它們工作正常。 只是在這里,在這個 class 中,在這個 loadContent 方法中,它們將不起作用。
只是為了添加dowhilefor 的出色答案,如果您想加載原始.jpg
文件(或.png
),您可以這樣做:
using(var s = File.OpenRead(fileName))
{
Texture2D texture = Texture2D.FromStream(GraphicsDevice, s);
}
與使用ContentManager
加載某些內容不同,在這種情況下您“擁有”它。 這意味着您有責任在UnloadContent
中調用Dispose()
。
與您通過內容管道(使用默認設置)時 go 不同,您加載的紋理不會具有預乘 alpha。 您需要自己應用預乘,或使用BlendState.NonPremultiplied
渲染它。
當然,除非出於某種原因(例如:您正在從 Internet 下載圖像,或者您讓最終用戶選擇它們),否則您應該使用內容管道。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.