0

Yeah, I know what you think, probably something like "Don't you have wrong path? -_-" And I can assure you, I have right path. Not saying it was working all the time, but when the exception pops up, and I will choose to continue, the program normally continues like nothing happened. Even the images, which had "wrong path" were loaded and drawn correctly. Even though I think that there is something messed up in the actual Monogame I'll give you my codes:

Image class (only piece of code, the class is pretty big)

    public float Alpha;
    public String FontName, Text;
    public String Path;
    [XmlIgnore]
    public Texture2D Texture;
    public Vector2 Scale;
    public Vector2 Position;
    [XmlIgnore]
    public Rectangle SourceRect;

    public bool CenterScale;
    public bool IsActive;

    SpriteFont font;
    Vector2 origin;
    ContentManager content;
    RenderTarget2D renderTarget;

    Dictionary<String, ImageEffect> effectList;

    public Image(String path) {
        Initialize(path);
    }

    public Image () {
        Initialize(String.Empty);
    }

    void Initialize(string path) {
        Text = Path = String.Empty;
        Path = path;
        FontName = "Arial";
        Position = Vector2.Zero;
        Scale = Vector2.One;
        Alpha = 1.0F;
        SourceRect = Rectangle.Empty;
        CenterScale = true;
        effectList = new Dictionary<String, ImageEffect>();
    }

    public void LoadContent () {
        content = new ContentManager(ScreenManager.Instance.Content.ServiceProvider, "Content");

        if (Path != String.Empty)
            Texture = content.Load<Texture2D>(Path); //error here

        font = content.Load<SpriteFont>(FontName);

        Vector2 dimensions = Vector2.Zero;

        if (Texture != null)
            dimensions.X += Texture.Width;
        dimensions.X += font.MeasureString(Text).X;

        if (Texture != null)
            dimensions.Y += Math.Max(Texture.Height, font.MeasureString(Text).Y);
        else
            dimensions.Y += font.MeasureString(Text).Y;

        if (SourceRect == Rectangle.Empty)
            SourceRect = new Rectangle(0, 0, (int)dimensions.X, (int)dimensions.Y);

        if (dimensions.X == 0)
            dimensions.X++;
        if (dimensions.Y == 0)
            dimensions.Y++;

        renderTarget = new RenderTarget2D(ScreenManager.Instance.GraphicsDevice, (int)dimensions.X, (int)dimensions.Y);

        ScreenManager.Instance.GraphicsDevice.SetRenderTarget(renderTarget);
        ScreenManager.Instance.GraphicsDevice.Clear(Color.Transparent);
        ScreenManager.Instance.SpriteBatch.Begin();
        if (Texture != null)
            ScreenManager.Instance.SpriteBatch.Draw(Texture, Vector2.Zero, Color.White);
        ScreenManager.Instance.SpriteBatch.DrawString(font, Text, Vector2.Zero, Color.White);
        ScreenManager.Instance.SpriteBatch.End();

        Texture = renderTarget;

        ScreenManager.Instance.GraphicsDevice.SetRenderTarget(null);
    }

And Image creation:

    Background = new Image("Backgrounds/MenuBackground.png");
    Background.LoadContent();       

This is my Content folder:
Tree
I think I have canceled some "Symbol loading" or somethink like that in Visual Studio. Could this cause that exception?

Valli3
  • 153
  • 2
  • 12
  • Right click on the MenuBackground.jpg, select properties, change Build Action to Content and Copy to Output Directory to Copy if Newer – GMich Oct 12 '15 at 02:09

2 Answers2

1

Monogame has a new (Depending on your definition of new) way of adding content to your project, which is through the Monogame Content Pipeline tool. It should be located here if you're using Windows:

C:\Program Files (x86)\MSBuild\MonoGame\v3.0\Tools

or

C:\Program Files\MSBuild\MonoGame\v3.0\Tools

Open the .mgcb Content Pipeline file with the Pipeline tool, add all the textures, and build them. (.mgcb by default should be inside your Content folder!)

Gertyerteg
  • 11
  • 1
  • Just add that you need to build all your resources which not .xnb, and replace all your old .png, .jpg and other with built .xnb's from /bin directory. Also, don't forget to set Build Action of new files to Content and Copy to Output Directory to Copy if Newer. – Sir D Apr 07 '17 at 09:37
0

Okay, I wasn't working on this project for 2 days and when I came back it seems to work again. I think that computer restart helped.

Valli3
  • 153
  • 2
  • 12