尝试处理图像时出现异常。 [英] Exception when trying to dispose a image.

查看:81
本文介绍了尝试处理图像时出现异常。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试处理以下代码的图像时将异常视为参数无效。

我在代码中使用了dispose方法而不是使用。抛出参数无效的异常。



不使用img.Dispose()和using语句,它工作正常。



Getting Exception as "Parameter is invalid" when trying to dispose the image for the following code.
Instead of using, i used dispose method in my code. It is throwing the same exception as "Parameter is invalid".

Without using img.Dispose() and using statement, it is working fine.

using (Image img = new Bitmap(images[currentCount]))
            {
                this.pictureBox1.Image = img;
            }







private void forwardTimer_Tick(object sender, EventArgs e)
       {
           if (pictureBox1.Image != null)
           {
               this.pictureBox1.Image.Dispose();
               this.pictureBox1.Image = null;

           } // if

           //
           // operation to be performed after the specified time elapses.
           //
           if (currentCount < numberOfImages - 1)
           {
               Image img = new Bitmap(images[currentCount + 1]);
               this.pictureBox1.Image = img;
               //this.pictureBox1.Image = images[currentCount + 1];
               currentCount++;
               this.countLabel.Text = string.Format("{0}/{1}", (currentCount + 1), numberOfImages);
               if (img != null)
                   ((IDisposable)img).Dispose();
           }// if
           else if (numberOfImages - 1 == currentCount)
           {
               currentCount = 0;
               Image img = new Bitmap(images[currentCount]);
               this.pictureBox1.Image = img;
               //this.pictureBox1.Image = images[currentCount];
               this.countLabel.Text = string.Format("{0}/{1}", (currentCount + 1), numberOfImages);
               if (img != null)
                   ((IDisposable)img).Dispose();
           }// else if

       }<pre lang="c#">

推荐答案

嗯...

使用并弃置扔掉你创建的实例,并将空间用于其他目的 - 这就是使用它们的重点。



但是如果你保持像你一样(在你的PictureBox中)对被处理实例的引用然后当它试图绘制图像时,它发现它不再存在,并且在那时你得到例外。



只有当你完全完成它时才能处理一个对象 - 或者你总会遇到问题。
Um...
using and Dispose "throw away" the instance that you created and free the space for other purposes - that is the whole point of using them.

But if you keep a reference to the disposed instance as you do (in your PictureBox) then when it tries to draw the image, it finds that it doesn't exist any more and at that point you get the exception.

You can only ever Dispose an object when you are finished with it completely - or you will always get problems.


来自Tomas的评论给出了错误,我认为这就是你的意思



The comment from Tomas gives the mistake and I think this is what you meant to do

private void LoadImage(Image newImage) {
  Image previousImage = pbox.Image;
  pbox.Image = newImage;
  if (previousImage != null) {
    previousImage.Dispose();
  }
}





你可以处理上一张图片,但只有在它之后由图片盒发布。



Alan。



You can dispose of the previous image but only after it has been released by the picturebox.

Alan.


这篇关于尝试处理图像时出现异常。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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