C# winform窗口打开关闭后不释放内存问题

问题解决一:

如果是窗体属性加载了背景图导致的内存占用,在关闭窗体前,释放掉背景图资源即可释放占用的内存

        private Image backgroundImage;
        public Form2()
        {
            InitializeComponent();

            backgroundImage = Image.FromFile(@"D:\XXXX.png"); //图片路径
            this.BackgroundImage = backgroundImage;
            this.BackgroundImageLayout = ImageLayout.Stretch;
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);

            // 释放图片资源
            if (backgroundImage != null)
            {
                backgroundImage.Dispose();
                backgroundImage = null;
            }
        }

 

posted @ 2024-03-27 09:18  芈璐  阅读(354)  评论(0编辑  收藏  举报