private static Bitmap ConvertTexture2DToBitmap(AssetItem item) { var converter = new Texture2DConverter((Texture2D)item.Asset); var bitmap = converter.ConvertToBitmap(true); return(bitmap); }
private static Bitmap CutImage(Texture2D m_Texture2D, Sprite m_Sprite, RectangleF textureRect, Vector2 textureRectOffset, SpriteSettings settingsRaw) { var texture2D = new Texture2DConverter(m_Texture2D); var originalImage = texture2D.ConvertToBitmap(false); if (originalImage != null) { using (originalImage) { //var spriteImage = originalImage.Clone(textureRect, PixelFormat.Format32bppArgb); var textureRectI = Rectangle.Round(textureRect); var spriteImage = new Bitmap(textureRectI.Width, textureRectI.Height, PixelFormat.Format32bppArgb); var destRect = new Rectangle(0, 0, textureRectI.Width, textureRectI.Height); using (var graphic = Graphics.FromImage(spriteImage)) { graphic.DrawImage(originalImage, destRect, textureRectI, GraphicsUnit.Pixel); } if (settingsRaw.packed == 1) { //RotateAndFlip switch (settingsRaw.packingRotation) { case SpritePackingRotation.kSPRFlipHorizontal: spriteImage.RotateFlip(RotateFlipType.RotateNoneFlipX); break; case SpritePackingRotation.kSPRFlipVertical: spriteImage.RotateFlip(RotateFlipType.RotateNoneFlipY); break; case SpritePackingRotation.kSPRRotate180: spriteImage.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case SpritePackingRotation.kSPRRotate90: spriteImage.RotateFlip(RotateFlipType.Rotate270FlipNone); break; } //Tight if (settingsRaw.packingMode == SpritePackingMode.kSPMTight) { try { var triangles = GetTriangles(m_Sprite.m_RD); var points = triangles.Select(x => x.Select(y => new PointF(y.X, y.Y)).ToArray()); using (var path = new GraphicsPath()) { foreach (var p in points) { path.AddPolygon(p); } using (var matr = new Matrix()) { var version = m_Sprite.version; if (version[0] < 5 || (version[0] == 5 && version[1] < 4) || (version[0] == 5 && version[1] == 4 && version[2] <= 1)) //5.4.1p3 down { matr.Translate(m_Sprite.m_Rect.Width * 0.5f - textureRectOffset.X, m_Sprite.m_Rect.Height * 0.5f - textureRectOffset.Y); } else { matr.Translate(m_Sprite.m_Rect.Width * m_Sprite.m_Pivot.X - textureRectOffset.X, m_Sprite.m_Rect.Height * m_Sprite.m_Pivot.Y - textureRectOffset.Y); } matr.Scale(m_Sprite.m_PixelsToUnits, m_Sprite.m_PixelsToUnits); path.Transform(matr); var bitmap = new Bitmap(textureRectI.Width, textureRectI.Height); using (var graphic = Graphics.FromImage(bitmap)) { using (var brush = new TextureBrush(spriteImage)) { graphic.FillPath(brush, path); bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY); return(bitmap); } } } } } catch { // ignored } } } //Rectangle spriteImage.RotateFlip(RotateFlipType.RotateNoneFlipY); return(spriteImage); } } return(null); }