How to prevent PNG from being converted to PNG.XNB ?

I am a beginner, and I use monogamous recently. I would like to display an image on the screen, simply.
How I’m trying to do it:
Texture2D image; string path; public override void LoadContent() { base.LoadContent(); path = "C:\\Users\\Quidam\\source\\repos\\LearningGameDev\\LearningGameDev\\Content\\SplashScreen\\Image1.png"; image = content.Load<Texture2D>(path); }

I have put my image inside the project like this:

And when I run the code I get this error:

Microsoft.Xna.Framework.Content.ContentLoadException: ‘The content file was not found.’
Message=The content file was not found.
at Microsoft.Xna.Framework.Content.ContentManager.OpenStream(String assetName)
at Microsoft.Xna.Framework.Content.ContentManager.ReadAsset[T](String assetName, Action`1 recordDisposableObject)
at Microsoft.Xna.Framework.Content.ContentManager.Load[T](String assetName)
at LearningGameDev.SplashScreen.LoadContent() in C:\Users\Quidam\source\repos\LearningGameDev\LearningGameDev\SplashScreen.cs:line 20
at LearningGameDev.ScreenManager.LoadContent(ContentManager Content) in C:\Users\Quidam\source\repos\LearningGameDev\LearningGameDev\ScreenManager.cs:line 40
at LearningGameDev.Game1.LoadContent() in C:\Users\Quidam\source\repos\LearningGameDev\LearningGameDev\Game1.cs:line 45
at Microsoft.Xna.Framework.Game.Initialize()
at LearningGameDev.Game1.Initialize() in C:\Users\Quidam\source\repos\LearningGameDev\LearningGameDev\Game1.cs:line 33
at Microsoft.Xna.Framework.Game.DoInitialize()
at Microsoft.Xna.Framework.Game.Run(GameRunBehavior runBehavior)
at LearningGameDev.Program.Main() in C:\Users\Quidam\source\repos\LearningGameDev\LearningGameDev\Program.cs:line 17

Inner Exception 1:
FileNotFoundException: Could not find file ‘C:\Users\Quidam\source\repos\LearningGameDev\LearningGameDev\Content\SplashScreen\Image1.png.xnb’.

instead of

path = “C:\Users\Quidam\source\repos\LearningGameDev\LearningGameDev\Content\SplashScreen\Image1.png”;

Double click on Content.mgcb and add your image1.png there.

path = “Content\SplashScreen\Image1”; //Without .png

ContentLoader can only load xnb files. To load unprocessed image files use Texture2D.FromStream and pass a file stream.

@kosmonautgames still does not work
@Jjagg how can I do that exactly ?

Texure2D texture;
using (var fs = System.IO.File.OpenRead(pngPath))
    texture = Texture2D.FromStream(GraphicsDevice, fs);

You should have a really good reason to do this, tho. Otherwise, use Pipeline.

Care to explain in more detail WHY? what makes the content pipeline better than loading in the png file? At least with the png files users can alter them to “mod” the game.

There are plenty of reasons.

  • What if you don’t want someone to mod it?
  • Loading content will also be platform-independent when using the Content Manager.
  • You get a tool for managing your content (not only textures, but organizing all your content)

There’s no “why” or “why not”. They’re two different tools for different purposes.

Personally I use raw JPG/PNG when I’m loading external files (i.e. downloaded from web) or when the XNB equivalent takes a LOT of memory compared to a PNG (I’ve had pixel art spritesheets that took 20x in XNB)

Also when prototyping I have my code reading first for JPG/PNG in local folder and going to the Content otherwise. This way I avoid having to manually add all the files I need.

The only real advantage the content pipeline has is that content loading is faster (i.e. when you load a PNG you must parse it and convert to a native texture format, while the content pipeline does this on compile time) and this really shines when targetting low powered devices like most androids. I must say I’m not a big fan of the content pipeline though. :slight_smile:

btw, you can also mod the XNB, it’s just less convenient than taking Photoshop and editing the PNG file.

For modding purposes, you could let users insert .png files and build them to .xnb when required.

PNG file is a lossless file format it uses LZW compression when written in physical storage that’s why it’s small in size compare when compiled to XNB.

PNG file compiled to XNB is much larger in size because its UNCOMPRESSED when written to disk for the purpose of NOT decompressing it on loading time meaning it’s much FASTER.

Normally our physical storage is much larger than our memory I couldn’t care less the size on disk as long it’s much faster in loading time.

EDIT : Other than that, we have an option to select Color key, Generate mip maps, make it square or resize to power of two and other fancy things the pipeline tool has to offer : ))

Cheers ^_^Y

Thanks all for the feedback to my query, much appreciated.

1 Like