Skip to content

Instantly share code, notes, and snippets.

@MnemonicWME
Created August 9, 2018 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MnemonicWME/4b6a3dbba16be8e1cec65e6248026b56 to your computer and use it in GitHub Desktop.
Save MnemonicWME/4b6a3dbba16be8e1cec65e6248026b56 to your computer and use it in GitHub Desktop.
//////////////////////////////////////////////////////////////////////////
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