MonoGame - Change Display Resolution
By Stephen Armstrong // February 25, 2020
Add a resolution selector to your game.
MonoGame’s default resolution is 800x480, and you can change it by using the GraphicsDeviceManager.
The following C# code will show you how to change the display resolution of a MonoGame project.
Starting up
After you create a MonoGame project, a GraphicsDeviceManager named “graphics” will be created.
Look for the following code in the Game1 constructor:
graphics = new GraphicsDeviceManager(this);
Once the GraphicsDeviceManager is created you can change the resolution as needed.
Change the starting resolution
Add the following lines of code after “graphics” is created to change the starting resolution:
// Change the resolution to 720p
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
graphics.ApplyChanges();
If you run the game it will now be displayed at 720p (1280 x 720).
Alternatively, you can use the following code to set the game to your desktop’s current resolution:
// If you have not used graphics yet, then using GraphicsDevice will crash the game. Get around this by calling ApplyChanges().
if (GraphicsDevice == null)
{
graphics.ApplyChanges();
}
// Change the resolution to match your current desktop
graphics.PreferredBackBufferWidth = GraphicsDevice.Adapter.CurrentDisplayMode.Width;
graphics.PreferredBackBufferHeight = GraphicsDevice.Adapter.CurrentDisplayMode.Height;
graphics.ApplyChanges();
If you run the game now, its resolution will match your current desktop resolution.
Create a resolution selector
It is essential that your game supports multiple resolutions and aspect ratios. This tutorial will show you how to make a basic resolution selector for your game.
Add two fields (both are ushort[ ]) to your Game1:
ushort [] widths;
ushort [] heights;
Now go to your Game1 constructor and add the following code:
// These arrays contain resolution data. Use the same index on them to get a resolution.
widths = new ushort [] { 3840, 2560, 2560, 1920, 1366, 1280, 1280 };
heights = new ushort [] { 2160, 1440, 1080, 1080, 768, 1024, 720 };
Add the following method to your Game1:
void ChangeResolution(byte newResolution)
{
// Only change resolution if the newResolution is between 0 and the length of the widths array
if (newResolution >= 0 && newResolution < widths.Length)
{
// Change the width and height to the new values in the array
graphics.PreferredBackBufferWidth = widths[newResolution];
graphics.PreferredBackBufferHeight = heights[newResolution];
// Apply the changes
graphics.ApplyChanges();
System.Console.WriteLine("New resolution: {0} x {1}", graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
}
}
Calling this method with a valid byte will immediately change the resolution.
Testing
To test that this method works, go to Game1.cs’ Update() cycle and add the following code:
// This Keys [] array and for loop is a small input handler to reduce the number of if statements
Keys [] FunctionKeys = new Keys [] { Keys.F1, Keys.F2, Keys.F3, Keys.F4, Keys.F5, Keys.F6, Keys.F7 };
// This for loop will check if one of the keys in the FunctionKeys array is pressed.
for (byte i = 0; i < FunctionKeys.Length; i++)
{
// If one of the keys in FunctionKeys is pressed it will change the resolution based on the index of that key
if (Keyboard.GetState().IsKeyDown(FunctionKeys[i]))
{
ChangeResolution(i);
}
}
Now run your game, and you will be able to change the resolution by pressing F1 - F7.
The resolution will also be displayed in the Output.
You can add more resolutions to the width and height arrays as needed to support a wide range of resolutions and aspect ratios.