public bool isImageChange(Rectangle pre, Rectangle current)
        {
            //定义两个Bitmap图像
            Bitmap preImage = new Bitmap(240, 320);
            Bitmap currentImage = new Bitmap(240, 320);            //System.Drawing.Imaging.BitmapData preImageData = preImage.LockBits(pre, System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            //System.Drawing.Imaging.BitmapData currentImageData = currentImage.LockBits(current, System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppRgb);            Graphics preGraph = Graphics.FromImage(preImage);
            Graphics currentGraph = Graphics.FromImage(currentImage);            preGraph.CopyFromScreen(pre.X, pre.Y, 0, 0, new Size(240, 320));
  ******    currentGraph.CopyFromScreen(current.X, current.Y, 0, 0, new Size(240, 320));            preGraph.Dispose();
            currentGraph.Dispose();            //对两个bitmap进行比较
            if (BitmapCompare2(preImage, currentImage) == 0)
            {
                //如果等于0,说明图像一致,返回true
                return true;
            }
            else
            {
                //如果不等于0,说明图像不一致,返回false
                return false;
            }
        }
我有一个timer,20毫秒执行一次,调用上面这个方法进行图片的对比刚开始的时候不会出错,时间长了就会在第二个CopyFromScreen出现“参数无效”的报错,不知道怎么回事儿

解决方案 »

  1.   

    有的时候还会出现这个错误“缓冲操作当前正在进行中,无法释放 BufferedGraphicsContext”
    今天把里面的有关graph的内容都注释了,就不出错了
      

  2.   

    在dispose之后,添加GC.Collect();
      

  3.   


    加上这个还是会出问题,就是上面说的“缓冲操作当前正在进行中,无法释放 BufferedGraphicsContext。”会不会是我这个timer执行的太快了,导致释放的速度比创建的速度慢呢???