How to use Texture2D.FromStream

I need to load an image (could be BMP, could be JPG, could be PNG) on the fly. What is the correct method to do it? This isn’t working (I think because spriteBatch isn’t a ‘device’)

                FileStream titleStream = File.OpenRead(File.OpenRead(Path.Combine(FilesIO.MapsFolder, MapFileName));
                TheMap = Texture2D.FromStream(spriteBatch, titleStream);
                titleStream.Close();

Or could you please steer me to some example code? Thanks.

You’ll need to get the GraphicsDevice from either your Game or your GraphicsDeviceManager

public class Game1 : Game
{
    // Graphics.GraphicsDevice and our Game1's GraphicsDevice are the same reference
    public GraphicsDeviceManager Graphics { get; private set; }

    public Game1()
    {
        Graphics = new GraphicsDeviceManager(this);
    }

    protected override void Initialize()
    {
        FileStream titleStream = File.OpenRead(File.OpenRead(Path.Combine(FilesIO.MapsFolder, MapFileName));
        // either work 
        TheMap = Texture2D.FromStream(GraphicsDevice, titleStream);
        TheMap = Texture2D.FromStream(Graphics.GraphicsDevice, tileStream);
        titleStream.Close();
    }
}

Edit: I also forgot that SpriteBatch holds a reference to the GraphicsDevice that you pass into the constructor so this will also work

TheMap = Texture2D.FromStream(spriteBatch.GraphicsDevice, tileStream);

Alternatively simply use Texture2D.FromFile()