public frmSpriteView(string fileName, bool isNew)
        {
            InitializeComponent();

            _fileName = fileName;

            if (!isNew)
            {
                Sprite sprite = new Sprite();
                sprite.Load(fileName);

                if (sprite.SpriteBitmap != null)
                {
                    txtFrameWidth.Text = sprite.Header.FrameWidth.ToString();
                    txtFrameHeight.Text = sprite.Header.FrameHeight.ToString();
                    txtFramesCount.Text = sprite.Header.FramesCount.ToString();
                    txtLines.Text = sprite.Header.Lines.ToString();
                    txtColor.BackColor = Color.FromArgb(sprite.Header.TransparentColorR, sprite.Header.TransparentColorG, sprite.Header.TransparentColorB);
                    pictureBox.Image = sprite.SpriteBitmap;

                }
            }
            else
            {
                pictureBox.ImageLocation = fileName;
            }
        }
Example #2
0
 public MainForm()
 {
     InitializeComponent();
     gr = this.panel1.CreateGraphics();
     Sprite s = new Sprite();
     s.SpriteBitmap = sprite;
     s.Load("e:\\3.spr");
     s.ApplyTransparentColor();
     sprite = s.SpriteBitmap;
     frameCount = 0;
     timer1.Start();
     timer2.Start();
     _x = panel1.Location.X;
     _y = panel1.Location.Y;
 }
        public override void Save(string fileName)
        {
            Sprite sprite = new Sprite();
            SpriteHeader header = new SpriteHeader();

            UInt16 width = 0;
            UInt16 height = 0;
            byte count = 0;
            byte lines = 0;

            if (!UInt16.TryParse(txtFrameWidth.Text, out width))
            {
                errorProvider.SetError(txtFrameWidth, "Incorrect value");
                return;
            }

            if (!UInt16.TryParse(txtFrameHeight.Text, out height))
            {
                errorProvider.SetError(txtFrameHeight, "Incorrect value");
                return;
            }

            if (!Byte.TryParse(txtFramesCount.Text, out count))
            {
                errorProvider.SetError(txtFramesCount, "Incorrect value");
                return;
            }

            if (!Byte.TryParse(txtLines.Text, out lines))
            {
                errorProvider.SetError(txtLines, "Incorrect value");
                return;
            }

            header.FrameWidth = width;
            header.FrameHeight = height;
            header.FramesCount = count;
            header.Lines = lines;
            header.TransparentColorR = txtColor.BackColor.R;
            header.TransparentColorG = txtColor.BackColor.G;
            header.TransparentColorB = txtColor.BackColor.B;
            header.Version = 255;

            sprite.Header = header;
            sprite.SpriteBitmap = new Bitmap(pictureBox.Image);
            sprite.Save(fileName);
        }
Example #4
0
        public bool Write(Sprite sprite)
        {
            if (sprite == null)
                return false;

            if (sprite.Header == null || sprite.SpriteBitmap == null)
                return false;

            using (BinaryWriter bw = new BinaryWriter(_s, Encoding.ASCII))
            {
                //Write header
                bw.Write(sprite.Header.Sign);
                bw.Write(sprite.Header.HeaderSize);
                bw.Write(sprite.Header.Version);
                bw.Write(sprite.Header.FrameWidth);
                bw.Write(sprite.Header.FrameHeight);
                bw.Write(sprite.Header.FramesCount);
                bw.Write(sprite.Header.Lines);
                bw.Write(sprite.Header.TransparentColorR);
                bw.Write(sprite.Header.TransparentColorG);
                bw.Write(sprite.Header.TransparentColorB);

                //WriteBitmap
                using(MemoryStream ms = new MemoryStream())
                {
                    sprite.SpriteBitmap.Save(ms, ImageFormat.Png);
                    ms.Flush();
                    ms.Seek(0, 0);
                    for (int i = 0; i < ms.Length; i++)
                    {
                        bw.Write((byte)ms.ReadByte());
                    }
                }

                bw.Flush();
            }

            return true;
        }
Example #5
0
        public Sprite Read()
        {
            if (_s == null)
                return null;

            Sprite sprite = new Sprite();
            sprite.Header = new SpriteHeader();

            using (BinaryReader br = new BinaryReader(_s, Encoding.ASCII))
            {
                char[] sign = br.ReadChars(3);
                if (sign.ToString() != sprite.Header.Sign.ToString())
                    return null;
                ushort headerSize = br.ReadUInt16();
                //Get version of sprite
                sprite.Header.Version = br.ReadByte();

                //TODO: read depense on version
                sprite.Header.FrameWidth = br.ReadUInt16();
                sprite.Header.FrameHeight = br.ReadUInt16();
                sprite.Header.FramesCount = br.ReadByte();
                sprite.Header.Lines = br.ReadByte();
                sprite.Header.TransparentColorR = br.ReadByte();
                sprite.Header.TransparentColorG = br.ReadByte();
                sprite.Header.TransparentColorB = br.ReadByte();

                using (MemoryStream ms = new MemoryStream())
                {
                    ms.Write(br.ReadBytes((int)(br.BaseStream.Length - headerSize)), 0, (int)(br.BaseStream.Length - headerSize));
                    ms.Flush();
                    sprite.SpriteBitmap = new Bitmap(Bitmap.FromStream(ms));
                }
            }

            return sprite;
        }