Texture2D.FromStream( ) no longer works...?

Hi :blush:
I have just reopened an old Monogame, windows- project, to continue working with it.
For some reason I can no longer run the project.
The line:

Texture2D texture = Texture2D.FromStream(Game1.CurrentGraphicsDevice, memoryStream);

fails with the message:“This image format is not supported”.
It does this with PNG, GIF, JPG, so I am convinced it has nothing to do with the internal image format.
The inner exception is from MonoGame.Utilities.ImageReader.Read and states: “unknown image type”.
On my machine, this minimal project can reproduce the problem. Can anyone confirm that it is a general problem, or just my setup?

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Drawing;
using System.IO;

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

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

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            using (var stream = new System.IO.FileStream("c:\\temp\\test.jpg", FileMode.Open))
            {
                using (Bitmap img = (Bitmap)Bitmap.FromStream(stream))
                {
                    var texture = Texture2D.FromStream(GraphicsDevice, stream);
                }
            }
        }
    }
}

Kind regards - Jakob

Hi,
Do you use Android/IOS backend of MG?
If, yes, then prob you’re experiencing this issue: #6507
It had been already fixed and fix would be available in the next version.

Hi - thanks for your input.
I’ve updated my original post, to reflect, that it is a Windows project.

Hi,
I was able to reproduce it. It seems there is a problem when a MemoryStream is passed to Texture2D.FromStream.
I’ve submitted a PR with fix: #6518

Thanks! :slight_smile:

Hm… I can see, that I am not using a memorystream in the codesample I’ve posted.
That’s a System.IO.FileStream.
I am actually not using the Bitmap for anything in the code…

Does your fix address the issue with FileStream as well?

Kind regards - Jakob

I can see there is something about context…

This works

using (var stream = new System.IO.FileStream("c:\\temp\\test.jpg", FileMode.Open))
{
    //using (Bitmap img = (Bitmap)Bitmap.FromStream(stream))
    {
        var texture = Texture2D.FromStream(GraphicsDevice, stream);
    }
}

This doesn’t

using (var stream = new System.IO.FileStream("c:\\temp\\test.jpg", FileMode.Open))
{
    using (Bitmap img = (Bitmap)Bitmap.FromStream(stream))
    {
        var texture = Texture2D.FromStream(GraphicsDevice, stream);
    }
}

Nope, FileStream is different issue. Problem is when you pass FileStream to the Texture2D.FromStream, it is already rewinded to the end. It could be fixed by adding FileStream.Seek call:

            using (var stream = new System.IO.FileStream("c:\\temp\\test.jpg", FileMode.Open))
            {
                using (Bitmap img = (Bitmap)Bitmap.FromStream(stream))
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    var texture = Texture2D.FromStream(GraphicsDevice, stream);
                }
            }

Or if you dont need Bitmap, above code could be simplified to:

            using (var stream = new System.IO.FileStream("c:\\temp\\test.jpg", FileMode.Open))
            {
                var texture = Texture2D.FromStream(GraphicsDevice, stream);
            }

And the rewind operation was JUST what was needed! Thanks :+1:
Here is the original code, with your fix:

MemoryStream memoryStream = new MemoryStream(bufferSize);
image.Save(memoryStream, ImageFormat.Png);
memoryStream.Seek(0, SeekOrigin.Begin);  //Your fix - thanks a bundle! :)
// Creates a texture from IO.Stream - our memory stream  
Texture2D texture = Texture2D.FromStream(Game1.CurrentGraphicsDevice, memoryStream);
1 Like

Thanks too for reporting this issue, as actually it seems like MonoGame has a bug.
As original XNA doesnt require stream to be rewinded for Texture2D.FromStream to work.

But when will new version achieve? Next year or after year?

You can download a develop version from the downloads page. We do not have a date set for the next public release.