Home > Net >  PictureBox images after the release, assignment again, a parameter is invalid
PictureBox images after the release, assignment again, a parameter is invalid

Time:11-23

 Image image1=(Image) _image. Clone (); 
If (this. PictureBox1. Image!=null)
{
Enclosing pictureBox1. Image. The Dispose ();
Enclosing pictureBox1. Image=null;
}
This. PictureBox1. Image=image1;


Call the dispose and assignment will be invalid parameter error, delete the dispose, normally won't appear error phenomenon,
What reason is this

CodePudding user response:

The measured, does not

CodePudding user response:

The actual test, there is no error

CodePudding user response:

refer to the second floor of responses asking, "what a long way to go back to go before
actual tested, no error

Loop assignment, an error will occur, plus the while (true)
Abnormal information:
* * * * * * * * * * * * * * exception text * * * * * * * * * * * * * *
System. ArgumentException: parameter is invalid,
In the System. Drawing. Image. Get_RawFormat ()
In the System. Drawing. Graphics. IgnoreMetafileErrors (Image Image, Int32 & amp; ErrorStatus)
In the System. Drawing. Graphics. DrawImage (Image Image, Int32 x, Int32 y, Int32 width, Int32 height)
In the System. Drawing. Graphics. DrawImage (Image Image, a Rectangle the rect)
In the System. Windows. Forms. PictureBox. OnPaint (PaintEventArgs PE)
In the System. Windows. Forms. Control. PaintWithErrorHandling (PaintEventArgs e, Int16 layer)
In the System. Windows. Forms. Control. WmPaint (Message& M)
In the System. Windows. Forms. Control. WndProc (Message& M)
In the System. Windows. Forms. Control. ControlNativeWindow. OnMessage (Message& M)
In the System. Windows. Forms. Control. ControlNativeWindow. WndProc (Message& M)
In the System. Windows. Forms. NativeWindow. Callback (IntPtr hWnd, Int32 MSG, IntPtr wparam, IntPtr lparam)

CodePudding user response:

Cycle, is a problem, analyze the code:

While (true)
{
If (this. PictureBox1. Image!=null)//cycle will not be called for the first time, enclosing pictureBox1. The Image is not the assignment, is null
{
Enclosing pictureBox1. Image. The Dispose ();
Enclosing pictureBox1. Image=null;
}

This. PictureBox1. Image=image1;
//will pictureBox1. Image address image1 object,
//because "Dispose" function is actually image1 instance, so the second cycle, then use to image1 object, will lose the wrong,
//should be changed to: this. PictureBox1. Image=image1 (Image). The Clone (); Create a image1 object instance,
Thread.sleep (100);
}

CodePudding user response:

This is following a cycle for many times, not the second cycle of
And it is in this. PictureBox1. Image=null; This is an error, added the try catch no catch

CodePudding user response:

Post code, enclosing pictureBox1. Image=null just the pictureBox members of the Image object pointing to the empty, not an error in theory,

CodePudding user response:

Public void ImageSet (Image Image)
{
If (image==null)
{
return;
}
Try
{
This. TopMost=true;
This. The Show ();
Enclosing BringToFront ();
_image=image;
While (true)
{
Image image1=(Image) _image. Clone ();

If (this. PictureBox1. Image!=null)
{
Enclosing pictureBox1. Image. The Dispose ();
Enclosing pictureBox1. Image=null;
}
This. PictureBox1. Image=image1;
}
}
Catch Exception (err)
{
Enclosing the Close ();
}
}

CodePudding user response:

If (this. PictureBox1. Image!=null)
{
Enclosing pictureBox1. Image. The Dispose ();
Enclosing pictureBox1. Image=null;
}

You remove the try

CodePudding user response:

refer to the eighth floor rabbit family - second brother reply:
if (this. PictureBox1. Image!=null)
{
Enclosing pictureBox1. Image. The Dispose ();
Enclosing pictureBox1. Image=null;
}

You remove the try


This after, ok, this is what reason is caused

CodePudding user response:

Enclosing pictureBox1. Image. The Dispose (); The image1 object is destroyed, then use this. PictureBox1. Image=image1; Not a problem to blame,

CodePudding user response:

But why go wrong cycle many times before

CodePudding user response:

To:

 public void ImageSet (Image Image) 
{
If (image==null)
{
return;
}
Try
{
This. TopMost=true;
This. The Show ();
Enclosing BringToFront ();
While (true)
{
If (this. PictureBox1. Image!=null)
{
Enclosing pictureBox1. Image. The Dispose ();
Enclosing pictureBox1. Image=null;
}
This. PictureBox1. Image=(Image) Image. The Clone ();
}
}
Catch Exception (err)
{
Enclosing the Close ();
}
}

CodePudding user response:

refer to 12th floor The kei ℡ response:
to:

 public void ImageSet (Image Image) 
{
If (image==null)
{
return;
}
Try
{
This. TopMost=true;
This. The Show ();
Enclosing BringToFront ();
While (true)
{
If (this. PictureBox1. Image!=null)
{
Enclosing pictureBox1. Image. The Dispose ();
Enclosing pictureBox1. Image=null;
}
This. PictureBox1. Image=(Image) Image. The Clone ();
}
}
Catch Exception (err)
{
Enclosing the Close ();
}
}


To this, will still be a problem, only the went to the out problems
 if (this. PictureBox1. Image!=null) 
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related