# Save Texture2D to file in Monogame

I'm working on a tilesheet builder tool for my game to speed up tile creation, and I need to save a generated texture2D as a png. I've tried using the .SaveAsPng function but it isn't implemented in Monogame.

Is there any way around this in monogame?

EDIT: I've tried this:

private void SaveTextureData(RenderTarget2D texture, string filename)
{
byte[] imageData = new byte[4 * texture.Width * texture.Height];
texture.GetData<byte>(imageData);

System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(texture.Width, texture.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
System.Drawing.Imaging.BitmapData bmData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, texture.Width, texture.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
IntPtr pnative = bmData.Scan0;
System.Runtime.InteropServices.Marshal.Copy(imageData, 0, pnative, 4 * texture.Width * texture.Height);
bitmap.UnlockBits(bmData);
bitmap.Save(filename);
}


but it's giving a weird output, and I don't know why: http://i.imgur.com/XT9Dnjj.png

Thanks for any help!

• Did you try debugging the issue? Verify your data is accurate, and that your methodology even works with known good data. Worst case scenario you have to write out the data manually. Bitmaps are a simple file format, here is the specification. daubnet.com/en/file-format-bmp – Evan Jul 28 '14 at 23:01