Image image1 = (Image)_image.Clone();
if (this.pictureBox1.Image != null)
{
this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = null;
}
this.pictureBox1.Image = image1;
呼叫dispose然后賦值會出現引數無效錯誤,把dispose洗掉之后,正常不會出現報錯現象,
這是什么原因
uj5u.com熱心網友回復:
實測,并不會uj5u.com熱心網友回復:
實際測驗了下,沒有報錯uj5u.com熱心網友回復:
回圈賦值的時候,就會出現報錯了,加上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)
uj5u.com熱心網友回復:
回圈的話,是會出問題,分析一下代碼: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);
}
uj5u.com熱心網友回復:
這個是回圈多次之后出現的,不是第二次回圈出現的而且是在this.pictureBox1.Image = null; 這一句就報錯了,加了try catch也沒有捕獲到
uj5u.com熱心網友回復:
貼代碼吧,this.pictureBox1.Image = null只是把pictureBox的Image成員物件指向空而已,理論上不會報錯。uj5u.com熱心網友回復:
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();
}
}
uj5u.com熱心網友回復:
if (this.pictureBox1.Image != null){
this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = null;
}
你把這個去掉試試呢
uj5u.com熱心網友回復:
這個去了之后,好了,這是什么原因造成的
uj5u.com熱心網友回復:
this.pictureBox1.Image.Dispose(); 這個把image1物件都銷毀了。再用this.pictureBox1.Image = image1; 不出問題才怪。uj5u.com熱心網友回復:
但是為什么回圈很多次之后才會出問題uj5u.com熱心網友回復:
建議改成: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();
}
}
uj5u.com熱心網友回復:
改成這樣,還是會出問題,只有把這個去了才會不出問題
if (this.pictureBox1.Image != null)
{
this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = null;
}
uj5u.com熱心網友回復:
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);
}
}
}
}
我這邊是鬧鬼了嗎!
uj5u.com熱心網友回復:
小兄弟,結帖吧。。。。。。。。。。
uj5u.com熱心網友回復:
大哥是否想討論有關元器件失效的問題?確實,一切事物都有其使用壽命,有關的問題涉及許多哲學根本問題的討論,比如:倫理,資源利用與再生,經濟學的金恒理論,。。。總之這個問題太復雜,你還是寫博吧。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/36313.html
標籤:C#
