/// <summary> /// Read from an existing atlas and decode it as multiple squaresprite; /// should contain one image and one meta file /// </summary> /// <param name="AtlasPath"> /// the path of atlas /// </param> /// <returns></returns> public static TexImage SingleReadFromAtlas(string AtlasPath, string SpriteName) { string AtlasMeta = AtlasPath + ".meta"; if (!File.Exists(AtlasPath)) { Console.WriteLine("atlas image lost!"); return(null); } if (!File.Exists(AtlasMeta)) { Console.WriteLine("atlas metafile lost!"); return(null); } Texture texture = TextureSet.Generate_texture(AtlasPath); TexImage texImage = null; BinaryReader metafile = new BinaryReader(File.Open(AtlasMeta, FileMode.Open)); TexImageInfo texImageInfo = new TexImageInfo(); while (metafile.BaseStream.Position != metafile.BaseStream.Length) { try { texImageInfo.BinaryRead(metafile); } catch (Exception ex) { Console.WriteLine("metafile corrupt!!" + ex.ToString()); return(null); } if (texImageInfo.filename == SpriteName) { texImage = new TexImage(texture, texImageInfo.filename, texImageInfo.LeftBottomLocation, texImageInfo.TopRightLocation); break; } } metafile.Close(); return(texImage); }
/// <summary> /// Read from an existing atlas and decode it as multiple bitmap /// should contain one image and one meta file /// </summary> /// <param name="AtlasPath"> /// the path of atlas /// </param> /// <returns></returns> public static Dictionary <string, Bitmap> ReadFromAtlasToBitmap(string AtlasPath) { string AtlasMeta = AtlasPath + ".meta"; if (!File.Exists(AtlasPath)) { Console.WriteLine("atlas image lost!"); return(null); } if (!File.Exists(AtlasMeta)) { Console.WriteLine("atlas metafile lost!"); return(null); } Dictionary <string, Bitmap> keyValuePairs = new Dictionary <string, Bitmap>(); Image <Rgba32> Origin_image = Image.Load <Rgba32>(AtlasPath); BinaryReader metafile = new BinaryReader(File.Open(AtlasMeta, FileMode.Open)); TexImageInfo texImageInfo = new TexImageInfo(); while (metafile.BaseStream.Position != metafile.BaseStream.Length) { try { texImageInfo.BinaryRead(metafile); } catch (Exception ex) { Console.WriteLine("metafile corrupt!!" + ex.ToString()); return(null); } int SubImageWidth = texImageInfo.TopRightLocation.X - texImageInfo.LeftBottomLocation.X + 1; int SubImageHeight = texImageInfo.TopRightLocation.Y - texImageInfo.LeftBottomLocation.Y + 1; Image <Rgba32> image = new Image <Rgba32>(SubImageWidth, SubImageHeight); for (int i = 0; i < SubImageWidth; i++) { for (int j = 0; j < SubImageHeight; j++) { try { image[i, j] = Origin_image[texImageInfo.LeftBottomLocation.X + i, Origin_image.Height - texImageInfo.TopRightLocation.Y + j]; } catch (Exception ex) { Console.WriteLine("error convert atlas to bitmap" + ex.ToString()); return(null); } } } //image.Mutate(i => i.Crop(new SixLabors.ImageSharp.Rectangle(LeftBottomLocation.X, Origin_image.Height - TopRightLocation.Y , SubImageWidth, SubImageHeight))); using (var memoryStream = new MemoryStream()) { image.Save(memoryStream, new PngEncoder()); memoryStream.Seek(0, SeekOrigin.Begin); keyValuePairs.Add(texImageInfo.filename, new Bitmap(memoryStream)); } image.Dispose(); } Origin_image.Dispose(); metafile.Close(); return(keyValuePairs); }