I'm making a particle engine so I develop a texture programatically and I want to display it in a Windows Form picture box. So I create the Texture2D which goes fine. Then I use this code to convert from a Texture2D to an Image for the picture box.
public static System.Drawing.Image Texture2Image(Texture2D texture)
{
if (texture.IsDisposed)
{
return null;
}
MemoryStream memoryStream = new MemoryStream();
texture.SaveAsPng(memoryStream, texture.Width, texture.Height);
memoryStream.Seek(0, SeekOrigin.Begin);
System.Drawing.Image image = System.Drawing.Bitmap.FromStream(memoryStream);
memoryStream.Close();
return image;
}
I get the error at the texture.SaveAsPng(...). It says 'Cannot access a disposed object. Object name: 'Texture2D'.'
But as you can see, I just checked to make sure it wasn't disposed. I looked at the Locals info which also says the IsDisposed flag is false. No where in my code to I call texture.Dispose() or anything of the sort. I have no idea where this error is coming from. Any help would be greatly appreciated, thanks in advance.
SaveAsPngline? It's likely you're not creating thetexture2Dcorrectly so you may want to show us how you're doing that. But first, use the debugger and break on theSaveAsPngline and check out what texture looks like. I'm voting to close as too localized. \$\endgroup\$