pictureBox释放图片之后,再赋值,出现参数无效

云顶F 2020-05-18 09:10:14
Image image1 = (Image)_image.Clone();
if (this.pictureBox1.Image != null)
{
this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = null;
}
this.pictureBox1.Image = image1;


调用dispose然后赋值会出现参数无效错误,把dispose删除之后,正常不会出现报错现象,
这是什么原因
...全文
1157 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
luj_1768 2020-05-24
  • 打赏
  • 举报
回复
大哥是否想讨论有关元器件失效的问题?确实,一切事物都有其使用寿命,有关的问题涉及许多哲学根本问题的讨论,比如:伦理,资源利用与再生,经济学的金恒理论,。。。 总之这个问题太复杂,你还是写博吧。
  • 打赏
  • 举报
回复
引用 楼主 云顶F 的回复:
Image image1 = (Image)_image.Clone();
if (this.pictureBox1.Image != null)
{
this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = null;
}
this.pictureBox1.Image = image1;


调用dispose然后赋值会出现参数无效错误,把dispose删除之后,正常不会出现报错现象,
这是什么原因

小兄弟,结帖吧。。。。。。。。。。
The 祺℡ 2020-05-21
  • 打赏
  • 举报
回复
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            Thread myThread = new Thread(new ParameterizedThreadStart(ImageSet));
            myThread.Start(Image.FromFile(@"C:\Users\hi\Desktop\1.bmp"));
        }

        public void ImageSet(object imageObj)
        {
            Image image = imageObj as Image;
            while (true)
            {
                if (pictureBox1.Image != null)
                {
                    pictureBox1.Image.Dispose();
                    pictureBox1.Image = null;
                }
                pictureBox1.Image = (Image)image.Clone();
                Thread.Sleep(200);
            }
        }
    }
}
我这边是闹鬼了吗!
云顶F 2020-05-21
  • 打赏
  • 举报
回复
引用 12 楼 The 祺℡ 的回复:
建议改成:
 public void ImageSet(Image image)
        {
            if (image == null)
            {
                return;
            }
            try
            {
                this.TopMost = true;
                this.Show();
                this.BringToFront();
                while (true)
                {
                    if (this.pictureBox1.Image != null)
                    {
                        this.pictureBox1.Image.Dispose();
                        this.pictureBox1.Image = null;
                    }
                    this.pictureBox1.Image = (Image)image.Clone();
                }
            }
            catch (Exception err)
            {
                this.Close();
            }
        }
改成这样,还是会出问题,只有把这个去了才会不出问题
if (this.pictureBox1.Image != null)
                    {
                        this.pictureBox1.Image.Dispose();
                        this.pictureBox1.Image = null;
                    }
The 祺℡ 2020-05-21
  • 打赏
  • 举报
回复
建议改成:
 public void ImageSet(Image image)
        {
            if (image == null)
            {
                return;
            }
            try
            {
                this.TopMost = true;
                this.Show();
                this.BringToFront();
                while (true)
                {
                    if (this.pictureBox1.Image != null)
                    {
                        this.pictureBox1.Image.Dispose();
                        this.pictureBox1.Image = null;
                    }
                    this.pictureBox1.Image = (Image)image.Clone();
                }
            }
            catch (Exception err)
            {
                this.Close();
            }
        }
云顶F 2020-05-21
  • 打赏
  • 举报
回复
但是为什么循环很多次之后才会出问题
The 祺℡ 2020-05-21
  • 打赏
  • 举报
回复
this.pictureBox1.Image.Dispose(); 这个把image1对象都销毁了。再用this.pictureBox1.Image = image1; 不出问题才怪。
云顶F 2020-05-21
  • 打赏
  • 举报
回复
引用 8 楼 兔子家族-二哥 的回复:
if (this.pictureBox1.Image != null) { this.pictureBox1.Image.Dispose(); this.pictureBox1.Image = null; } 你把这个去掉试试呢
这个去了之后,好了,这是什么原因造成的
  • 打赏
  • 举报
回复
if (this.pictureBox1.Image != null)
{
this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = null;
}

你把这个去掉试试呢
云顶F 2020-05-21
  • 打赏
  • 举报
回复
public void ImageSet(Image image) { if (image == null) { return; } try { this.TopMost = true; this.Show(); this.BringToFront(); _image = image; while (true) { Image image1 = (Image)_image.Clone(); if (this.pictureBox1.Image != null) { this.pictureBox1.Image.Dispose(); this.pictureBox1.Image = null; } this.pictureBox1.Image = image1; } } catch (Exception err) { this.Close(); } }
The 祺℡ 2020-05-20
  • 打赏
  • 举报
回复
贴代码吧,this.pictureBox1.Image = null只是把pictureBox的Image成员对象指向空而已,理论上不会报错。
The 祺℡ 2020-05-19
  • 打赏
  • 举报
回复
实测,并不会
云顶F 2020-05-19
  • 打赏
  • 举报
回复
这个是循环多次之后出现的,不是第二次循环出现的 而且是在this.pictureBox1.Image = null; 这一句就报错了,加了try catch也没有捕获到
The 祺℡ 2020-05-19
  • 打赏
  • 举报
回复
循环的话,是会出问题,分析一下代码: while (true) { if (this.pictureBox1.Image != null) //第一次循环不会被调用,this.pictureBox1.Image没被赋值,就是null的 { this.pictureBox1.Image.Dispose(); this.pictureBox1.Image = null; } this.pictureBox1.Image = image1; //将pictureBox1.Image指向image1对象地址。 //因为“Dispose”函数销毁的其实是image1实例,所以第二次循环,再使用到image1对象,会丢错。 //应该改为:this.pictureBox1.Image = (Image)image1.Clone();创建一个image1对象副本。 Thread.Sleep(100); }
云顶F 2020-05-19
  • 打赏
  • 举报
回复
引用 2 楼 前路漫漫回头走 的回复:
实际测试了下,没有报错
循环赋值的时候,就会出现报错了,加上while(true) 异常信息: ************** 异常文本 ************** System.ArgumentException: 参数无效。 在 System.Drawing.Image.get_RawFormat() 在 System.Drawing.Graphics.IgnoreMetafileErrors(Image image, Int32& errorStatus) 在 System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height) 在 System.Drawing.Graphics.DrawImage(Image image, Rectangle rect) 在 System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe) 在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer) 在 System.Windows.Forms.Control.WmPaint(Message& m) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  • 打赏
  • 举报
回复
实际测试了下,没有报错

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧