图标闪烁3981次就出现GDI异常

图标闪烁3981次就出现GDI错误
本帖最后由 kk86868058 于 2014-11-25 16:07:44 编辑
http://pan.baidu.com/s/1bneZSlD
以上是我的demo代码
点击开始闪烁后,等计数器跳到3981的时候会出现提示GDI一般性错误
求解决办法,我要实现图标闪烁来表达消息提示,但闪烁一段时间后用户都不点击闪烁图标去阅读消息的话,托盘图标就会变得不正常了


顺便贴一下代码,其实也很少:
一个NotifyIcon控件,一个ImageList,一个Timer和一个Button
Timer的周期本来是400的,为了快速呈现效果我的代码里改成了200

    public partial class Form1 : Form
    {
        private bool isShowEmpty = true;
        private int seconds = 0;

        public Form1()
        {
            InitializeComponent();

            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Text = (++this.seconds) + " 快到3981了!";

            Image img = imageList1.Images[this.isShowEmpty ? 0 : 1];
            Bitmap bmp = new Bitmap(img);
            notifyIcon1.Icon = Icon.FromHandle(bmp.GetHicon());

            this.isShowEmpty = !this.isShowEmpty;

        }
    }

------解决思路----------------------
应该是资源释放的问题。没设一次图标就创建了一个Icon的实例,还没有释放,不出错才怪呢。
看看MSDN是怎么说的:
“When using this method, you must dispose of the original icon by using the DestroyIcon method in the Win32 API to ensure that the resources are released.”

这里有完整示例:
http://msdn.microsoft.com/en-us/library/system.drawing.icon.fromhandle(v=vs.110).aspx