Okay, I'm writing a game in C#/XNA 4.0 and am utterly stumped at my current juncture: Sprite animation. I understand how it works and have all the code in place, but my ContentLoader won't open my file...

Basically, my directory looks like this:

      - "Game1.cs"
      - //Classes
          - "NPC.cs"
      - Content Reference
          - //Images
              - "Monster.png"

Inside my NPC class, I have all the essential drawing functions, i.e. LoadContent, Draw, Update. And I can get the game to find the correct file and attempt to open it, but when it tries, it throws an exception and tells me it can't open the file. This is how my code in my NPC class looks:

    Texture2D NPCImage;
    Vector2 NPCPosition;
    Animation NPCAnimation = new Animation();

    public void Initialize()
        NPCAnimation.Initialize(NPCPosition, new Vector2(4, 4));

    public void LoadContent(ContentManager Content)
        NPCImage = Content.Load<Texture2D>("_InsertImageFilePathHere_");
        NPCAnimation.AnimationImage = NPCImage;

The rest of the code is irrelevant at this point because I can't even get the image to load. I think it might have to do with a directory problem, but I also know little to nothing about spriting or working with images or animations in my code. Any help is appreciated. Not sure if I provided enough information here, so let me know if more is needed! Also, what would be the correct way to direct that Content.Load to Monster.png given the current directory situation? Right now I just have it using the full path from the C:// drive. Thanks in advance!

  • 1
    \$\begingroup\$ Actually what you insert in InsertImageFilePathHere is the path and assetname, in your case it would be "Images/Monster" because XNA would just trim the .png and use "Monster" as the default assetname \$\endgroup\$
    – Blu3
    Jun 30, 2013 at 11:33
  • \$\begingroup\$ Is that my problem though? \$\endgroup\$ Jun 30, 2013 at 17:07
  • \$\begingroup\$ What does the exception actually say? \$\endgroup\$ Jul 1, 2013 at 4:36
  • \$\begingroup\$ An unhandled exception of type 'Microsoft.Xna.Framework.Content.ContentLoadException' occurred in Microsoft.Xna.Framework.dll Additional information: Error loading "C:\Users\Donutdood\Documents\Visual Studio 2012\Projects\SwagDungeon\SwagDungeonStuff\Resources\Monsters\Boulder\Walking". Cannot open file. \$\endgroup\$ Jul 1, 2013 at 16:01

1 Answer 1


Make sure you include the path to the image (relative to the content folder) and do not include ".png" XNA will convert the .png to a .xnb (XNA binary) as part of the content pipeline, so you don't want to reference the .png portion of the file in your code.

On rare occasions I've also seen people with problems because of their PNG was corrupt. If the above doesn't fix the problem, try another png file... something simple you find from a quick google search.

  • \$\begingroup\$ Okay, here's my question: Is it possible to make an animation from a collection of images, rather than a giant string of them one after another in the same image file? Because the walking animations I have for some of my creatures consist of at least 20 images, and side by side, that's over 3,900 pixels... \$\endgroup\$ Jul 1, 2013 at 3:51
  • \$\begingroup\$ @user32623 It's best to ask a fresh question for something like that. But, yes, you can use separate image files if you like. You could also tile them in 2D, to stay below the maximum texture dimensions. \$\endgroup\$ Jul 1, 2013 at 4:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .