public static byte[] GetPNG(this Texture2D t2d, Texture2DMipInfo info) { PixelFormat format = Image.getPixelFormatType(t2d.TextureFormat); MemoryStream ms = new MemoryStream(); Image.convertToPng(Texture2D.GetTextureData(info, t2d.Export.Game), info.width, info.height, format).Save(ms); return(ms.ToArray()); }
/// <summary> /// Exports the texture to PNG format. Writes to the specified stream, or the specified path if not defined. /// </summary> /// <param name="outputPath"></param> /// <param name="outStream"></param> /// <returns></returns> public static bool ExportToPNG(this Texture2D t2d, string outputPath = null, Stream outStream = null) { if (outputPath == null && outStream == null) { throw new Exception("ExportToPNG() requires at least one not-null parameter."); } Texture2DMipInfo info = new Texture2DMipInfo(); info = t2d.Mips.FirstOrDefault(x => x.storageType != StorageTypes.empty); if (info != null) { byte[] imageBytes = null; try { imageBytes = Texture2D.GetTextureData(info, t2d.Export.Game); } catch (FileNotFoundException e) { Debug.WriteLine("External cache not found. Defaulting to internal mips."); //External archive not found - using built in mips (will be hideous, but better than nothing) info = t2d.Mips.FirstOrDefault(x => x.storageType == StorageTypes.pccUnc); if (info != null) { imageBytes = Texture2D.GetTextureData(info, t2d.Export.Game); } } if (imageBytes != null) { PixelFormat format = Image.getPixelFormatType(t2d.TextureFormat); PngBitmapEncoder image = Image.convertToPng(imageBytes, info.width, info.height, format); if (outStream == null) { outStream = new FileStream(outputPath, FileMode.Create); image.Save(outStream); outStream.Close(); } else { image.Save(outStream); } } } return(true); }