C# 製作ANI動畫光標的類

測試代碼

 

  private void Form2_Load(object sender, EventArgs e)
        {
            byte _ValueChar = (byte)'A';
            Zgke.MyImage.ImageFile.ImageAni _Ani = new Zgke.MyImage.ImageFile.ImageAni();
            for (int i = 0; i != 26; i++)
            {
                Bitmap _Bitmap = new Bitmap(32, 32);
                Graphics _Graphcis = Graphics.FromImage(_Bitmap);
                _Graphcis.DrawString(((char)_ValueChar).ToString(), new Font("黑體",20,FontStyle.Bold), Brushes.Yellow, 0, 0);
                _Graphcis.Dispose();

                _Ani.AniImage.Add(new Zgke.MyImage.ImageFile.ImageAni.ANIImage(_Bitmap, 10));
                _ValueChar++;               
            }

            _Ani.SaveFile(@"C:/1.ANI");

 

            this.Cursor = new Cursor(LoadCursorFromFile(@"C:/1.ANI"));


        }


        [DllImport("user32.dll", EntryPoint = "LoadCursorFromFile")]
        public static extern IntPtr LoadCursorFromFile(string lpFileName);

 

 

 

 

下面是全部的類

這個類需要使用CUR哪個類..有需要的可以去

http://blog.csdn.net/zgke/archive/2009/06/11/4260686.aspx 下載

 

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;


namespace Zgke.MyImage.ImageFile
{
    /// <summary>
    /// ANI操作類
    /// [email protected]
    /// qq:116149
    /// </summary>
    public class ImageAni
    {       
        /// <summary>
        /// 播放時間
        /// </summary>
        private class Rate
        {
            private byte[] m_Rate = new byte[] { 0x72, 0x61, 0x74, 0x65 };
            private byte[] m_RateSize = new byte[4];
            private List<int> m_ViewIndex = new List<int>();

            /// <summary>
            /// 顯示順序
            /// </summary>
            public List<int> ViewIndex
            {
                get { return m_ViewIndex; }
                set { m_ViewIndex = value; }
            }
        
            /// <summary>
            /// Rete大小
            /// </summary>
            public int ReteSize
            {
                get { return BitConverter.ToInt32(m_RateSize, 0); }
                set { m_RateSize = BitConverter.GetBytes(value); }
            }

            public Rate(byte[] p_FileBytes, ref int p_ReadIndex)
            {
                p_ReadIndex += 4;
                Array.Copy(p_FileBytes, p_ReadIndex, m_RateSize, 0, 4);  //獲取Anih大小
                p_ReadIndex += 4;
                for (int i = 0; i != ReteSize / 4; i++)
                {
                    m_ViewIndex.Add(BitConverter.ToInt32(p_FileBytes, p_ReadIndex));
                    p_ReadIndex += 4;
                }
            }

            public Rate()
            {
            }

            public byte[] GetBytes()
            {
                MemoryStream _Memory = new MemoryStream();

                _Memory.Write(m_Rate, 0, 4);
                _Memory.Write(BitConverter.GetBytes(ViewIndex.Count*4), 0, 4);
                for (int i = 0; i != m_ViewIndex.Count; i++)
                {
                    _Memory.Write(BitConverter.GetBytes(m_ViewIndex[i]), 0, 4);
                }
                return _Memory.ToArray();
            }
        }

        /// <summary>
        /// 文件播放順序
        /// </summary>
        private class Seq
        {
            private byte[] m_Seq = new byte[] { 0x73, 0x65, 0x71, 0x20 };
            private byte[] m_SeqSize = new byte[4];
            private List<int> m_PlayIndex = new List<int>();

            /// <summary>
            /// 播放順序
            /// </summary>
            public List<int> PlayIndex
            {
                get { return m_PlayIndex; }
                set { m_PlayIndex = value; }
            }
   
            /// <summary>
            ///  Seq大小
            /// </summary>
            public int SeqSize
            {
                get { return BitConverter.ToInt32(m_SeqSize, 0); }
                set { m_SeqSize = BitConverter.GetBytes(value); }
            }

            public Seq(byte[] p_FileBytes, ref int p_ReadIndex)
            {
                p_ReadIndex += 4;
                Array.Copy(p_FileBytes, p_ReadIndex, m_SeqSize, 0, 4);  //獲取Anih大小
                p_ReadIndex+=4;
                for (int i = 0; i != SeqSize / 4; i++)
                {
                    m_PlayIndex.Add(BitConverter.ToInt32(p_FileBytes, p_ReadIndex));
                    p_ReadIndex += 4;
                }
            }

            public Seq()
            {
            }

            public byte[] GetBytes()
            {
                MemoryStream _Memory = new MemoryStream();

                _Memory.Write(m_Seq, 0, 4);
                _Memory.Write(BitConverter.GetBytes(PlayIndex.Count * 4), 0, 4);
                for (int i = 0; i != m_PlayIndex.Count; i++)
                {
                    _Memory.Write(BitConverter.GetBytes(m_PlayIndex[i]), 0, 4);
                }
                return _Memory.ToArray();
            }
        }

        /// <summary>
        /// 文件基本信息
        /// </summary>
        private class AniH
        {
            private byte[] m_Anih = new byte[] { 0x61, 0x6E, 0x69, 0x68 };

            private byte[] m_AnihSize = new byte[4];          

            private byte[] m_24 = new byte[] { 0x24, 0x00, 0x00, 0x00 };

            private byte[] m_ImageCount = new byte[] { 0x24, 0x00, 0x00, 0x00 };         

            private byte[] m_ImagePlayCount = new byte[] { 0x24, 0x00, 0x00, 0x00 };

            private byte[] m_NotUser = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00 };

            /// <summary>
            /// 播放數量
            /// </summary>
            public int ImagePlayCount
            {
                get { return BitConverter.ToInt32(m_ImagePlayCount,0); }
                set { m_ImagePlayCount = BitConverter.GetBytes(value); }
            }
            /// <summary>
            /// 圖形數量
            /// </summary>
            public int ImageCount
            {
                get { return BitConverter.ToInt32(m_ImageCount, 0); }
                set { m_ImageCount = BitConverter.GetBytes(value); }
            }
            /// <summary>
            /// ANI大小
            /// </summary>
            public int AnihSize
            {
                get { return BitConverter.ToInt32(m_AnihSize, 0); }
                set { m_AnihSize = BitConverter.GetBytes(value); }
            }

            public AniH(byte[] p_FileBytes,ref int p_ReadIndex)
            {
                p_ReadIndex += 4;
                Array.Copy(p_FileBytes, p_ReadIndex,m_AnihSize, 0, 4);  //獲取Anih大小
                p_ReadIndex += 8;   //m_24;

                Array.Copy(p_FileBytes, p_ReadIndex, m_ImageCount, 0, 4);  //獲取Anih大小
                p_ReadIndex += 4;
                Array.Copy(p_FileBytes, p_ReadIndex, m_ImagePlayCount, 0, 4);  //獲取Anih大小
                p_ReadIndex += 4;

                m_NotUser = new byte[AnihSize - 12];
                Array.Copy(p_FileBytes, p_ReadIndex, m_NotUser, 0,m_NotUser.Length);
                p_ReadIndex += m_NotUser.Length;
            }

            public AniH(int p_ImageCount)
            {
                ImagePlayCount = p_ImageCount;
                ImageCount = p_ImageCount;
                m_AnihSize = BitConverter.GetBytes(36);
            }

            public byte[] GetBytes()
            {
                byte[] _ReturnBytes = new byte[44];
                Array.Copy(m_Anih, 0, _ReturnBytes, 0, 4);
                Array.Copy(m_AnihSize, 0, _ReturnBytes, 4, 4);
                Array.Copy(m_24, 0, _ReturnBytes, 8, 4);
                Array.Copy(m_ImageCount, 0, _ReturnBytes, 12, 4);
                Array.Copy(m_ImagePlayCount, 0, _ReturnBytes, 16, 4);
                Array.Copy(m_NotUser, 0, _ReturnBytes, 20, m_NotUser.Length);
                return _ReturnBytes;
            }
        }

        /// <summary>
        /// 描述信息
        /// </summary>
        private class ListInfo
        {
            private byte[] m_List = new byte[] { 0x4C, 0x49, 0x53, 0x54 };
            private byte[] m_ListSize = new byte[4];
            private byte[] m_Data;
         
            public int ListSize
            {
                get { return BitConverter.ToInt32(m_ListSize, 0); }
                set { m_ListSize = BitConverter.GetBytes(value); }
            }

            public ListInfo(byte[] p_FileBytes, ref int p_ReadIndex)
            {
                p_ReadIndex += 4;
                Array.Copy(p_FileBytes, p_ReadIndex, m_ListSize, 0, 4);
                m_Data = new byte[ListSize];
                p_ReadIndex += 4;
                Array.Copy(p_FileBytes, p_ReadIndex, m_Data, 0, m_Data.Length);

                p_ReadIndex += m_Data.Length;
            }
        }


        public class ANIImage
        {
            private Image m_SetImage;

            public Image SetImage
            {
                get { return m_SetImage; }
                set { m_SetImage = value; }
            }

            private int m_SetTime = 1;

            public int SetTime
            {
                get { return m_SetTime; }
                set { m_SetTime = value; }
            }

            public ANIImage(Image p_Image, int p_SetTime)
            {
                if (p_Image == null) throw new Exception("不能添加空");
                m_SetImage = p_Image;
                m_SetTime = p_SetTime;
            }
        }

        private AniH m_Anih;
        private Seq m_Seq; 
        private ListInfo m_ListInfo;
        private Rate m_Rate;


        private IList<ANIImage> m_AniImage = new List<ANIImage>();

        public IList<ANIImage> AniImage
        {
            get { return m_AniImage; }
            set { m_AniImage = value; }
        }
   
        /// <summary>
        /// 文件當前位置
        /// </summary>
        private int _ReadIndex = 0;


      
        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="p_File"></param>
        public void SaveFile(string p_FileFullName)
        {
            if (AniImage.Count == 0) return;
            FileStream _File = new FileStream(p_FileFullName, FileMode.Create, FileAccess.ReadWrite);

            MemoryStream _Memory = new MemoryStream();
           
            Rate _Rate = new Rate();
            Seq _Seq =new Seq();
    
            for (int i = 0; i != m_AniImage.Count; i++)
            {
                ImageCur _Cur = new ImageCur();
                _Cur.CurImage.Add(m_AniImage[i].SetImage);
                MemoryStream _CurBytes = _Cur.SaveImage(true);
                byte[] _ImageBytes = _CurBytes.ToArray();
                _Memory.Write(new byte[] { 0x69, 0x63, 0x6F, 0x6E }, 0, 4);
                _Memory.Write(BitConverter.GetBytes(_ImageBytes.Length), 0, 4);
                _Memory.Write(_ImageBytes, 0, _ImageBytes.Length);

                _Rate.ViewIndex.Add(m_AniImage[i].SetTime);
                _Seq.PlayIndex.Add(i);
            }        

            AniH _Anih =new AniH(m_AniImage.Count);
            byte[] _AnihBytes =_Anih.GetBytes();
            byte[] _RateBytes = _Rate.GetBytes();
            byte[] _SeqBytes =_Seq.GetBytes();

            int _Size = _AnihBytes.Length + _RateBytes.Length + (int)_Memory.Length + 24 + _SeqBytes.Length; //12個開始 12個List....fram

            _File.Write(m_RIFF, 0, m_RIFF.Length); //寫文件頭
            _File.Write(BitConverter.GetBytes(_Size), 0, 4);
            _File.Write(m_ACON, 0, 4);
            _File.Write(_AnihBytes, 0, _AnihBytes.Length);
            _File.Write(_RateBytes, 0, _RateBytes.Length);
            _File.Write(_SeqBytes, 0, _SeqBytes.Length);

            _File.Write(new byte[] { 0x4C, 0x49, 0x53, 0x54 }, 0, 4);
            _File.Write(BitConverter.GetBytes((int)_Memory.Length+4), 0, 4);
            _File.Write(new byte[] { 0x66, 0x72, 0x61, 0x6D }, 0, 4);
            _File.Write(_Memory.ToArray(), 0, (int)_Memory.Length);
            _File.Close();
         }

        private byte[] m_RIFF = new byte[ ] { 0x52, 0x49, 0x46, 0x46 };
        private byte[] m_FileSize = new byte[4];
        private byte[] m_ACON = new byte[] { 0x41, 0x43, 0x4F, 0x4E };

        public ImageAni(string p_FileFullName)
        {
            byte[] _FileBytes = File.ReadAllBytes(p_FileFullName);

            if (_FileBytes[_ReadIndex] == m_RIFF[0] && _FileBytes[_ReadIndex + 1] == m_RIFF[1] && _FileBytes[_ReadIndex + 2] == m_RIFF[2] && _FileBytes[_ReadIndex + 3] == m_RIFF[3])
            {
                _ReadIndex += 4;
                Array.Copy(_FileBytes, _ReadIndex, m_FileSize, 0, 4);  //獲取文件大小
                _ReadIndex+=4;

                 if (_FileBytes[_ReadIndex] == m_ACON[0] && _FileBytes[_ReadIndex + 1] == m_ACON[1] && _FileBytes[_ReadIndex + 2] == m_ACON[2] && _FileBytes[_ReadIndex + 3] == m_ACON[3])
                 {
                     _ReadIndex+=4;
                     while (true)
                     {
                         if (_ReadIndex >= _FileBytes.Length) break;
                         string _Value = System.Text.Encoding.ASCII.GetString(_FileBytes, _ReadIndex, 4);

                         switch (_Value)
                         {
                             case "anih":
                                 m_Anih = new AniH(_FileBytes,ref  _ReadIndex);
                                 break;
                             case "seq ":
                                 m_Seq = new Seq(_FileBytes, ref _ReadIndex);
                                 break;
                             case "LIST":                       
                                 switch (System.Text.Encoding.ASCII.GetString(_FileBytes, _ReadIndex+8, 4))
                                 {
                                     case "fram":
                                         _ReadIndex += 12;                                       
                                         break;
                                     case "INFO":
                                         m_ListInfo = new ListInfo(_FileBytes, ref _ReadIndex);
                                         break;
                                     default:
                                         throw new Exception("錯誤的文件!");
                                 }
                                 break;
                             case "rate":
                                 m_Rate = new Rate(_FileBytes, ref _ReadIndex);
                                 break;
                             case "icon":
                                 _ReadIndex += 4;
                                 int _IconSize =BitConverter.ToInt32(_FileBytes,_ReadIndex);
                                 _ReadIndex+=4;
                                 byte[] _IconByte = new byte[_IconSize];                              
                                 Array.Copy(_FileBytes, _ReadIndex, _IconByte,0, _IconByte.Length);
                                 ImageCur _Cur = new ImageCur(_IconByte);
                                 for(int i=0;i!=_Cur.CurImage.Count;i++)
                                 {
                                     m_AniImage.Add(new ANIImage(_Cur.CurImage[i], 1));                                   
                                 }
                                 _ReadIndex += _IconSize;
                                 break;
                             default:
                                 throw new Exception("錯誤的文件!");
                         }
                     }
                 }
                 else
                 {
                      throw new Exception("文件缺少ACON");
                 }  
            }
            else
            {
                throw new Exception("文件頭不是RIFF開頭");
            }        
        }

        public ImageAni()
        {
        }
    }
}

 

發佈了95 篇原創文章 · 獲贊 17 · 訪問量 56萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章