Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//////////////////////////////////////////////////////////////////////////
public static System.Drawing.Image TextureToImage(SmartTexture texture)
{
// SaveAsPng is buggy in monogame (it's actually a compression bug in ZlibStream)
//Image img;
//using (MemoryStream ms = new MemoryStream())
//{
// texture.SaveAsPng(ms, texture.Width, texture.Height);
// ms.Seek(0, SeekOrigin.Begin);
// img = Image.FromStream(ms);
//}
//return img;
uint[] textureData = new uint[texture.Value.Width * texture.Value.Height];
texture.Value.GetData<uint>(textureData);
Bitmap bmp = new Bitmap(texture.Value.Width, texture.Value.Height, PixelFormat.Format32bppArgb);
unsafe
{
BitmapData origdata = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
uint* byteData = (uint*)origdata.Scan0;
for (int i = 0; i < textureData.Length; i++)
{
byteData[i] = (textureData[i] & 0x000000ff) << 16 | (textureData[i] & 0x0000FF00) | (textureData[i] & 0x00FF0000) >> 16 | (textureData[i] & 0xFF000000);
}
bmp.UnlockBits(origdata);
}
return bmp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.