C#Dispose函数给出错误 [英] C# Dispose function giving an error

查看:162
本文介绍了C#Dispose函数给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图每10毫秒拍摄一次屏幕截图,并将其设置为带有Timer的Picturebox.image.几秒钟后,程序可以完美运行,但几秒钟后,程序崩溃了.我试图在代码末尾使用Dispose()函数来清除内存,但Dispose Function也给出了错误. (增加计时器间隔无效)

I am trying to take screenshot for every 10 miliseconds and set them as a Picturebox.image with Timer. For few seconds program runs perfectly but after few seconds program is crashing. I tried to use Dispose() Function in the end of the code to clear the memory but Dispose Function also gives an error. (increasing interval of timer doesn't worked)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace gameBot
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public Bitmap screenshot;
        Graphics GFX;
        private void button1_Click(object sender, EventArgs e)
        {
            timer1.enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            takescreenshot();
        }
        private void takescreenshot()
        {

            screenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
            Screen.PrimaryScreen.Bounds.Height);
            GFX = Graphics.FromImage(screenshot);
            GFX.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size);
            pictureBox1.Image = screenshot;
            screenshot.Dispose();              
        }           
    }
}

错误是

在System.Drawing.dll中发生了'System.ArgumentException'类型的未处理异常

"An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll

其他信息:参数无效."

Additional information: Parameter is not valid."

在崩溃之前,程序也使用了过多的RAM(也许是由于内存不足异常而崩溃了?) 您可以在此处看到

Also program is using too much RAM before crashing (Maybe it's crashing because of out of memory exception?) As you can see in here

推荐答案

请参见

您应始终调用Dispose方法以释放Graphics和 FromImage方法创建的相关资源.

You should always call the Dispose method to release the Graphics and related resources created by the FromImage method.

此外,也不需要将这些Graphics保持在Class级别.

Also, there's no need to keep that Graphics at Class level.

考虑到这一点,您所需要做的就是:

With that in mind, all you need is:

public Bitmap screenshot;

private void takescreenshot()
{
    if (screenshot != null)
    {
        screenshot.Dispose();
    }

    screenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    using (Graphics GFX = Graphics.FromImage(screenshot))
    {
        GFX.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size);
    }
    pictureBox1.Image = screenshot;
}

这篇关于C#Dispose函数给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆