C# MonoGame Help? (Content.Load<Texture2D>("Invader");)

11,514

Solution 1

I am trying to load a texture that I have added to the Content folder (It is a PNG file called "Invader") invader = Content.Load("Invader");

Actually, you can load the PNG content that has been added to the Content folder directly like so:

invader = Content.Load<Texture2D>("Invader");

Note that the filename is case senitive on some platforms so be careful that it matches exactly. Also, make sure you've set the file to Content / Copy if newer in the Properties window.

The alternative is to compile your assets into optimized binary XNB files using the XNA Game Studio Content Pipeline or the MonoGame Content Pipeline. This will give you better performance but carries extra development overhead.

I should also mention that when rendering your sprites as raw PNG files you should use BlendState.NonPremultiplied in the call to SpriteBatch.Begin for best results. I've been doing it this way in my games for a while and I'm pretty happy with the results.

Solution 2

Make sure your Build Action is set to Content for the .png in question. Do this by right clicking the file and selecting properties.

Solution 3

MonoGame does not fully implement the content manager. Typically, you build the content separately, and import the built content files into your project. Then you can load them as usual.

To build the content files, you can use an XNA or MonoGame content builder such as this one. If you prefer, you can use command lines as part of your project's build process so that content is built automatically.

Share:
11,514
user2635139
Author by

user2635139

Updated on June 05, 2022

Comments

  • user2635139
    user2635139 almost 2 years

    I am Making a Space Invaders Game using Open GL in MonoGame and I am trying to load a texture that I have added to the Content folder (It is a PNG file called "Invader")
    The code that I use is:

    invader = Content.Load<Texture2D>("Invader");
    

    However when I attempt to run it It says:

    ContentLoadException was unhandled
    could not load Invader as a non-content file!

    • ZeroPhase
      ZeroPhase over 10 years
      I believe the content manager isn't fully implemented in MonoGame. If you share more code I might be able to help out some more.
  • ZeroPhase
    ZeroPhase over 10 years
    I believe there is a fork with a functional content manager. It might be MonoGame with PCL support. I haven't used MonoGame, yet, but they did mention there is a way to implement it / we can work on developing the content manager. Let me look through my notes.
  • josaphatv
    josaphatv over 10 years
    +1 because this is exactly what the problem is. Also, make sure to set the action to "Copy if newer" or "Copy always."
  • josaphatv
    josaphatv over 10 years
    You should not use the extension. Monogame will first look for .xnb files and if it can't find them fall back on known native formats if they are supported. See here.
  • craftworkgames
    craftworkgames over 10 years
    I made a correction, the extension is not required. I swear I tried this a few months back and it didn't work, but it does now so I apologize for the previously misleading answer.