Issue with MonoGame 3.8 and SaveAsPng/SaveAsJpeg

Loving 3.8 so far. Lots of great updates! I ran into one thing I did not expect. A pre-existing function that would save a RenderTarget2D to a png/jpeg no longer works as intended. Immediately after upgrading my project to 3.8, the images started coming out like this:
Earth

That is suppose to be an image of the Earth, but it looks like an only TV that is out of sync now. The same image, when rendered to screen, looks like this…

Earth

Thoughts?

Is this a 2D or 3D project? Just because it works well with GL/DX with 2D texture. I tested with this simple code:

public class Game1 : Game
{
    private GraphicsDeviceManager graphics;
    private SpriteBatch spriteBatch;

    Texture2D texture;
    Stream stream;
    RenderTarget2D renderTarget;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        IsMouseVisible = true;
    }

    protected override void Initialize()
    {
        graphics.PreferredBackBufferWidth = 360;
        graphics.PreferredBackBufferHeight = 349;
        graphics.SynchronizeWithVerticalRetrace = false;
        IsFixedTimeStep = true;
        IsMouseVisible = true;
        graphics.ApplyChanges();

        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);

        texture = Content.Load<Texture2D>("earth");

        renderTarget = new RenderTarget2D(GraphicsDevice, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
        {
            stream = File.Create("Content/file.png");
            renderTarget.SaveAsPng(stream, renderTarget.Width, renderTarget.Height);
            stream.Dispose();
            stream = File.Create("Content/file.jpg");
            renderTarget.SaveAsJpeg(stream, renderTarget.Width, renderTarget.Height);
            stream.Dispose();
            Exit();
        }



        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.SetRenderTarget(renderTarget);
        //GraphicsDevice.DepthStencilState = new DepthStencilState() { DepthBufferEnable = true };
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin(samplerState: SamplerState.PointClamp, blendState: BlendState.NonPremultiplied);
        spriteBatch.Draw(texture, Vector2.Zero, Color.White);
        spriteBatch.End();

        GraphicsDevice.SetRenderTarget(null);

        spriteBatch.Begin(samplerState: SamplerState.PointClamp);
        spriteBatch.Draw(renderTarget, new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight), Color.White);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}

It’s a DirectX 3D project. The same code that generated the image correctly before produces the odd results you see once the project was upgraded to 3.8. Very strange.

I’d recommend making a minimal sample that reproduces the problem and opening an issue at github.

1 Like