Search code examples
c#xnaspritemonogame

Monogame not drawing png images


I'm trying to draw a sprite to the screen in monogame but does not work. Does not give any errors. I've tried other images and it works with them. Maybe it has something to do with the image being .png? Please help me I can't figure this out

Code:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Test
{
    public class Game1 : Game
    {
        private GraphicsDeviceManager _graphics;
        private SpriteBatch _spriteBatch;
        
        Texture2D realisticSturgeonSprite;

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

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

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

            
            realisticSturgeonSprite = Content.Load<Texture2D>("Sturgeon");
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            Texture2D rect = new Texture2D(_graphics.GraphicsDevice, 80, 30);

            Color[] data = new Color[80 * 30];
            for (int i = 0; i < data.Length; i++) data[i] = Color.Chocolate;
            rect.SetData(data);

            Vector2 coor = new Vector2(10, 20);
            _spriteBatch.Begin();
            _spriteBatch.Draw(rect, coor, Color.White);
            _spriteBatch.Draw(realisticSturgeonSprite, new Vector2(50, 60), Color.White);
            _spriteBatch.Draw(sturgeonSprite, new Vector2(80, 90), Color.White);
            _spriteBatch.End();
            

            base.Draw(gameTime);
        }
    }
}

Sprite: Lake_Sturgeon.png

I can add more details to the post if there are any that I left out.


Solution

  • It is easier to use the Monogame Pipline tool to process the PNG file into an XNB file first. Best cross-platform solution.


    There may be times you need to load a PNG directly.

    Add an existing file to the project in Visual Studio. Select the PNG file. Copy it.

    Under properties in Visual Studio, set "Build type to none" and "Copy to output if newer".

    Note this only works on Desktop platforms.

    protected override void LoadContent()
            {
                _spriteBatch = new SpriteBatch(GraphicsDevice);
    
                
                realisticSturgeonSprite = Texture2D.FromFile(GraphicsDevice,@"Lake_Sturgeon.png"); 
      // you can add path if needed (copy to output option above doesn't need it)
            }
    
    
            protected override void Draw(GameTime gameTime)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);
    
                _spriteBatch.Begin();
    
                _spriteBatch.Draw(realisticSturgeonSprite, new Vector2(50, 60), Color.White);
    
                _spriteBatch.End();
               
    
                base.Draw(gameTime);
            }
    

    Draw the texture as you would normally.