Created
August 9, 2018 06:02
-
-
Save MnemonicWME/4b6a3dbba16be8e1cec65e6248026b56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////////////////////////////////////////////// | |
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