Monogame Texture2D.FromStream() memory leak

First off, I can’t use Monogames Pipeline because I am creating a game engine, there will be a folder of textures(so .png’s .jpegs etc). I have asked a similar question before about White Pixels with Texture2D.FromStream() and someone posted that they just added textures directly to the pipeline and well I have no clue how to do that at run time; So I am left with From Stream.

Here is the code, it gets rid of the whitepixels that FromStream() comes with:
public static Texture2D ConvertTexture(string filePath, CludoGame game) {
Texture2D texture = null;
try {
using(FileStream d = new FileStream(filePath, FileMode.Open)) {
texture = Texture2D.FromStream(game.GraphicsDevice, d);
Color[] data = new Color[texture.Width * texture.Height];
for(int i = 0; i != data.Length; ++i)
data[i] = Color.FromNonPremultiplied(data[i].ToVector4());
data = null;
} catch(System.IO.DirectoryNotFoundException) {
if(File.Exists(filePath)) {
throw new ArgumentException(“Access to: " + filePath + " is denied!”);
} else {
throw new ArgumentException(“File: " + filePath + " doesn’t exist! Make sure the file isn’t just a directory, or you have typed the filePath correctly.”);
return texture;

On the line of texture = Texture2D.FromStream(game.GraphicsDevice, d); the ram usage jumps up(around 30 mb’s) for a single texture. Remember that games use a lot of textures and this happens for EVERY texture(The texture is 10mb but using the monogame pipeline in another game it doesn’t do this jump…). So what can I do from this point? I’m really lost.


Just put the files in the MonoGame Content Manager (Add, Existing File). It generates .xnb files.

Alternatively, Monogame can still use non xnb format files, just include the same file name, it will look for .xnb first, then grab any files with same name of another extension though i do not know the hierarchy of extensions it will load in order…

Just load the textures normally, you dont have to do it from a stream.

Thank you :). Can’t believe it was that easy… plus now I have the advantage of the Monogame pipeline!

Your welcome! Glad I could help.