Exception: parameter is invalid (when transferring a new image to pictureBox)

I already had an image inside the PictureBox control, and now I want to transfer a new one to it.

What happens is that allpication Dispos (and I catch the exception: "The parameter is not valid").

This is my code:

using (Image img = Image.FromFile(open.FileName)) { part.Picture = img; pictureBox1.InitialImage = null; pictureBox1.Image = img; } 

So, when the code exits the method, it becomes open to Displose this and the main form. I will catch the exception only on the line where Form1 was run. There are no exceptions to this that have ever been so. It must be something wrong while the pictureBox is being drawn (inside the Paint event), but I'm not distracted by it.

I really don't know how to solve this problem. I even tried to use to clear all resources (causing garbage collection), but nothing works.


One more thing: "part" is a link in the List, so when I try to delete the current image (to replace it with a new one), I got another exception, for example:

"The process cannot access the file because it is being used by another process."


Is this due to the 1st exception (when the new image is not painted in the pictureBox)?

+8
c # image dispose
source share
4 answers

As Reid noted, the image you pull from open.Filename is called after you exit the using () statement. Your camera still refers to this image in memory, so when it is located, you lose what was stored in your image box.

What you really need is a unique copy of the image you are pulling.

  using (Image sourceImg = Image.FromFile(open.Filename)) { Image clonedImg = new Bitmap(sourceImg.Width, sourceImg.Height, PixelFormat.Format32bppArgb); using (var copy = Graphics.FromImage(clonedImg)) { copy.DrawImage(sourceImg, 0, 0); } pictureBox1.InitialImage = null; pictureBox1.Image = clonedImg; } 

Thus, your file will be unlocked as soon as you exit this block and you will save a unique copy of your image in the frame.

+8
source share

The problem is that after executing this code, pictureBox1.Image refers to the Image that was deleted.

If you do not complete the creation of Image in using , it should fix your problem.

 Image img = Image.FromFile(open.FileName); part.Picture = img; pictureBox1.InitialImage = null; pictureBox1.Image = img; // You can't dispose of this, or it won't be valid when PictureBox uses it! 
+4
source share

You can also do something like the following: create a method that uploads the images and then passes it back to Image Control, for example, this is what I use when I want to fill out Image Ctrl

I have a window shape with three different images that I want to upload, but I only show the code for One, since I call the same method for all 3 image controls

  #region Codes for browsing for a picture /// <summary> /// this.picStudent the name of the Image Control /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnStudentPic_Click(object sender, EventArgs e) { Image picture = (Image)BrowseForPicture(); this.picStudent.Image = picture; this.picStudent.SizeMode = PictureBoxSizeMode.StretchImage; } /// <summary> /// /// </summary> /// <returns></returns> private Bitmap BrowseForPicture() { // Bitmap picture = null; try { if (this.fdlgStudentPic.ShowDialog() == DialogResult.OK) { byte[] imageBytes = File.ReadAllBytes(this.fdlgStudentPic.FileName); StudentPic = new Bitmap( this.fdlgStudentPic.FileName); StuInfo.StudentPic = imageBytes; } else { StudentPic = Properties.Resources.NoPhotoAvailable; } } catch (Exception) { MessageBox.Show("That was not a picture.", "Browse for picture"); StudentPic = this.BrowseForPicture(); } return StudentPic; } #endregion 
0
source share

Yes, now it works, but it’s strange, I would almost swear that I tried too. Well, never mind, it just works. I'm worried about the same thing, which, in my opinion, is the same as your code, but it doesn’t work, the application tries to recycle it again (with the same exception). This is a sample code:

 using(Image img = Image.FromFile(open.FileName)) { part.Picture = img; } pictureBox1.InitialImage = null; pictureBox1.Image = part.Picture; //Picture is a propery in a class 

Now I pass the actual image to the general list and try to assign a new pictureBox to it, but, as I said, an exception is thrown (and the application terminates). Why?

0
source share

All Articles