Example #1
0
        private static Texture LoadSpriteSheetAbsolute(string title, string file_name)
        {
            // Load and decompress Photoshop file structures
            var psdFile = new PsdFile();

            psdFile.Load(file_name, Encoding.Default);

            if (psdFile.Layers.Count == 0)
            {
                psdFile.BaseLayer.CreateMissingChannels();
            }
            var layer = psdFile.BaseLayer;

            using (Bitmap bmp = new Bitmap(psdFile.ColumnCount, psdFile.RowCount)) {
                using (Graphics gfx = Graphics.FromImage(bmp)) {
                    gfx.Clear(Color.Transparent);
                    var bmp_data   = bmp.LockBits(layer.Rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
                    int pixel_size = 4 * sizeof(byte);
                    int idx        = 0;
                    for (int i = 0; i < bmp_data.Height; i++)
                    {
                        for (int j = 0; j < bmp_data.Width; j++)
                        {
                            Marshal.Copy(new byte[] {
                                layer.Channels [2].ImageData [idx],
                                layer.Channels [1].ImageData [idx],
                                layer.Channels [0].ImageData [idx],
                                layer.Channels.Count >= 4 ? layer.Channels [3].ImageData [idx] : (byte)0xFF,
                            }, 0, new IntPtr((long)bmp_data.Scan0 + i * bmp_data.Stride + j * pixel_size), pixel_size);
                            idx++;
                        }
                    }
                    bmp.UnlockBits(bmp_data);
                }
                var texture = Texture.LoadTexture(title, bmp);
                psdFile.SlicesToTextureRegionInfo(ref texture);
                return(texture);
            }
        }