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);
}
}
}
}
}
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.
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?
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);
}
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.