0

C# / .NET 3.5, WindowsForms.

I have this Windows form that displays an image from a file, and whenever user saves the record this code is executed:

                    string oldLoc = itemsBO.ImageLoc;
                    if (oldLoc != SystemSettings.NoImageLocation)
                    {
                        if (File.Exists(oldLoc))
                        {
                            try { File.Delete(oldLoc); }
                            catch (IOException ex)
                            {
                                MessageBox.Show("1 - "  + ex.GetType().ToString() + "    " + ex.Message);
                            }
                        }
                    }

                    string saveLoc = itemsBO.ImageSearchLoc + ".jpg";
                    if (File.Exists(saveLoc))
                    {
                        try { File.Delete(saveLoc); }
                        catch (IOException ex)
                        {
                            MessageBox.Show("2 - " + ex.GetType().ToString() + "    " + ex.Message);
                        }
                    }

                    try
                    {
                        if (pictureBox2.Image != null)
                            pictureBox2.Image.Save(saveLoc, System.Drawing.Imaging.ImageFormat.Jpeg);
                    }
                    catch (IOException ex)
                    {
                        MessageBox.Show("3 - " + ex.GetType().ToString() + "    " + ex.Message);
                    }

Disregard the poor MessageBox messages, but it errors out in each Catch statement. It can't delete the "existing" Image because it says it's in use by another process. Can't save because a file exists in that same path because it's not deleting.

This is the code that sets the Image when they try to add a new picture;

            Image clipImage = Clipboard.GetImage();

            if (tabControl2.SelectedTab == tabPage5)
            {
                pictureBox1.Image = clipImage;
                itemsBO.IsDirtyImage = true;
            }
            else if (tabControl2.SelectedTab == tabPage6)
            {
                pictureBox2.Image = clipImage;
                itemsBO.IsDirtyImage2 = true;
            }

Then when the form loads up an existing record with an image, this is the code used to fetch/display it:

            byte[] bits = File.ReadAllBytes(imgfil);
            msImage = new MemoryStream(bits, 0, bits.Length);

            if (tabControl2.SelectedTab == tabPage5)
                pictureBox1.Image = Image.FromStream(msImage);
            else if (tabControl2.SelectedTab == tabPage6)
                pictureBox2.Image = Image.FromStream(msImage);

imgfil being a path to the image, of course.

Absolutely no idea where to begin...

user1096207
  • 157
  • 2
  • 5
  • 12
  • Are you reading the file at this location somewhere in your code itemsBO.ImageLoc ? – Gaurav Sep 10 '13 at 21:50
  • ImageLoc is the path to the file, yes. – user1096207 Sep 10 '13 at 21:53
  • i meant are you loading the file at this location itemsBO.ImageLoc into your app? if Yes , how are you loading it – Gaurav Sep 10 '13 at 21:57
  • Is the creator of the file the same as the Account that the code is running under? In other words it probably does not have permission to delete the file. Suggest change permissions on the file and rerun your code. – miltonb Sep 10 '13 at 21:57
  • A lot of the times, there isn't necessarily a file yet created-- they take a screenshot using the Snipping tool in Win7, then just right-click --> copy without even saving and pasting it into the application. – user1096207 Sep 11 '13 at 13:18
  • and @Gaurav the last 2 chunks of code show how the images get added. The second snippit of code is when the user loads / creates a new record and Pastes an image from their clip board. The third snippit is when they load an existing record, that's how it loads the image based off itemsBO.ImageLoc – user1096207 Sep 11 '13 at 13:25

3 Answers3

0

I have this Windows form that displays an image from a file, and whenever user saves the record

If you're still displaying the image when they save the file, the application will still be accessing the file if I'm not mistaken. Try disposing of the file first, probably by setting the picture box's (or whatever you're using to display the image) image to null, or load a blank picture before you perform the operation.

floppsb
  • 696
  • 2
  • 9
  • 15
  • Tried changing it to throw the picturebox's image into a temporary Image object, and then set the picture box to null so I could save the temp instead of the picture box, but that yields the same results :( – user1096207 Sep 11 '13 at 13:41
  • @user1096207 is using File.ReadAllBytes(imgfil) method to load the image. This method opens the file, reads the contents of the file into a byte array, and then closes the file. So this is not the case. – Gaurav Sep 11 '13 at 15:59
0

If it says file in use by another process, well then it must be in use by another process :)

Have you tried monitoring the file lock using Process Explorer. Once you have identified what's holding your file, close that file handle using Process Explorer and then try to run your code.

This might help- How to find out what processes have folder or file locked?

Community
  • 1
  • 1
Gaurav
  • 840
  • 4
  • 16
0

So I had inherited this application from another user, turns out the pictureBoxes were having their Image set in another chunk of code independent of that third block of code in the original post. It was because of this that the IOException was happening :(

user1096207
  • 157
  • 2
  • 5
  • 12