Skip to main content
Up to date

This page is up to date for MonoGame.Extended 6.0.0. If you find outdated information, please open an issue.

ContentManager extensions

ContentManager.OpenStream​

System.IO.Stream ContentManager.OpenStream(string filename)

OpenStream allows easy access to load data from files. Files loaded this way should be in the content folder defined by Content.RootDirectory.

Example below loads a text file "song-lyrics" directly. This file is assumed to have its properties set to "Copy to Output Directory".

// Put this in your Game1 class
private string songLyrics;
// Put this in your Initialize() method
// song-lyrics.txt is in the Content Directory
using (var stream = Content.OpenStream("song-lyrics.txt"))
{
using (var reader = new StreamReader(stream))
{
songLyrics = reader.ReadToEnd();
}
}

In this next example, it is loading a PNG file, and creating a Texture2D from that stream. This file is also assumed to have it's properties set to "Copy to Output Directory".

// Put this in your Game1 class
private Texture2D monoTextureManual;
// Put this in your Initialize() method
// extendedlogo.png is in the Content Directory
using (var stream = Content.OpenStream("extendedlogo.png"))
{
monoTextureManual = Texture2D.FromStream(GraphicsDevice, stream);
}

ContentManager.GetGraphicsDevice​

GraphicsDevice ContentManager.GetGraphicsDevice()

GetGraphicsDevice returns the current GraphicsDevice from the services.

var graphicsDevice = Content.GetGraphicsDevice();
var width = graphicsDevice.DisplayMode.Width;