# XNA graphics.PreferredBackBufferWidth's new value doesn't immediately change window size

I set the width and height of the window in my game constructor, and then pass the graphics manager to a class to check the value. In the game's initialize function, it doesn't seem to recognize the new width and height values, so it uses the default.

Here's relevant code:

Game1.cs Constructor:

graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1600;
graphics.PreferredBackBufferHeight = 900;
Content.RootDirectory = "Content";
World = new TestWorld(Content, graphics);


Game1.cs Initialize

World.Initialize();


TestWorld Initialize

Debug.WriteLine(Graphics.GraphicsDevice.Viewport.Width);


The TestWorld Initialize outputs the default (800).

I have tried graphics.ApplyChanges(), but it does not resolve the issue.

• Have you tried to add graphics.ApplyChanges(); after? It should not be mandatory when changing size in the game constructor, but you can still try. Jul 29 '14 at 18:34
• As trivial of an answer that is it is still one. I'd recommend adding it as an answer. Jul 29 '14 at 18:36
• @AlphSpirit I have tried the apply changes in the constructor and the game1 initialize to no avail! Jul 29 '14 at 18:36
• @ClassicThunder See the comment from Shane. That's why I didn't post this as an answer, because I knew it was very unlikely to solve the problem. Jul 29 '14 at 18:39
• Using Graphics.PreferredBackbufferWidth over Graphics.GraphicsDevice.Viewport.Width works. It looks like the viewport doesn't change until base.initialize gets called. Jul 29 '14 at 18:56

Please take a look at this old answer of mine that explains the process in detail.

Basically: The members on GraphicsDeviceManager are settings that are only actually applied when ApplyChanges or CreateDevice is called. Note that CreateDevice will get called for you if you are using the built-in Game class.

Regarding additional questions asked in comments: The graphics device (and so GraphicsDevice.Viewport) is not guaranteed to be available until LoadContent gets called (ref).

Although you can usually get away with using it in Initialize, that is not strictly correct (it does not play nice if the graphics device gets reset).

The correct way to get the window/viewport size is described in detail in this answer.

I think you might need to restructure your code so that things get set up in the right order.

You need to call graphics.ApplyChanges(); in order to apply your updated settings.

• I've tried this already. Doesn't seem to help. I've added it in the Game1 Constructor and the World Initialize Jul 29 '14 at 18:39
• Strange, it works for me in my Initalize() function. It isn't called immediately, but at the end of my Initalize(), after logging messages to the log file and loading the settings config. Maybe you need to wait a little while before calling it.
– Seta
Jul 29 '14 at 18:41
• I wonder if the changes are applied in the base.Initialize() for the game class... Not sure how I would get around that without moving around my functions. Jul 29 '14 at 18:52