0

EDIT: I finally found the error. It is totally irrelevant with the bitmaps or streams or static. It appears that one of my colleagues has forgotten to remove email attachment after sending the mail, and the mail attachment service keeps open. I used a using statement for whole mail sending process, and it is solved. Thanks everyone.

I know you might say that there are billions of threads with the same title and this is a duplicate, but believe me it is not. I have been searching for the solution like 7 hours, but nothing helped so far.

The problem is the following: This is a photo capture application which uses WebcamSource as the webcam. The application runs well when first photo is taken and emailed to user. However, when user returns to the process all over again (where it started before the first run), application gives such a error. The erroneous code is below.

public static void SaveImageCapture(BitmapSource bitmap)
        {

                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
         //     bitmap = BitmapFrame.Create(BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                encoder.Frames.Add(BitmapFrame.Create(bitmap));
                encoder.QualityLevel = 100;
                encoder.Rotation = Rotation.Rotate270;
                try
                {
                    using (FileStream fstream = new FileStream("Photos\\" + fileName + ".jpg", FileMode.Create))
                    {
                        encoder.Save(fstream);
                        fstream.Close();

                    }
                }
                catch (Exception e) {
                    System.Windows.Forms.MessageBox.Show(e.ToString());
                }

        }

Code crashes at FileStream fstream = new FileStream("Photos\\" + fileName + ".jpg", FileMode.Create) and it gives the error

The process cannot access the file "C:\Users[username]\Dropbox[projectname][projectname]\bin\Debug\Photos" because it is being used by another process.

I tried closing webcam stream, surrounding the code with try/catch, putting it into using statement, adding FileAccess and FileShare fields, trying to add BitmapCreateOptions.None and BitmapCacheOption.OnLoad(it did not allow me), creating new images with different names rather than overwriting the same image, deleting image after sending email(it gave me the same error), and some small arrangements that may cause file access problems.

hevele
  • 903
  • 6
  • 20
  • 38
  • try renaming the image..to see if threads is holding to it. – Anand Aug 20 '13 at 13:28
  • I already tried that, I tried saving every image with different names. – hevele Aug 20 '13 at 13:29
  • 1
    isn't "fstream.close" redundant, since using(filestream) will close it anyway? – Raven Dreamer Aug 20 '13 at 13:31
  • I know that but I wanted to make sure that it will close it. I cant make sense of the error anyways. – hevele Aug 20 '13 at 13:35
  • Show how do you create the bitmap – meda Aug 20 '13 at 13:39
  • What do you mean by didn't allow me? What was the error? – sara Aug 20 '13 at 13:40
  • Where does `fileName` come from? I would suggest using `Path.Combine` to construct paths. Also perhaps `DropBox` is sync'ing the files and that is the _other process_. – Marc Aug 20 '13 at 13:42
  • The error is "The process cannot access the file "C:\Users[username]\Dropbox[projectname][projectname]\bin\Debug\Photos\[filename].jpg" because it is being used by another process." as I said in the question – hevele Aug 20 '13 at 13:43
  • @Marc I tried moving the folder outside of Dropbox folder. It is the same as in Dropbox. – hevele Aug 20 '13 at 13:43
  • What is calling your `SaveImageCapture` method? Are you trying to save the image back to the same file it was loaded from? – Jim Mischel Aug 20 '13 at 13:51
  • Have you seen http://stackoverflow.com/questions/4527356/c-sharp-the-process-cannot-access-the-file-because-it-is-being-used-by-another? – sara Aug 20 '13 at 14:15
  • @sara It didn't allow me to add a BitmapCacheOption. I am using BitmapSource, not BitmapImage. I assume it doesn't allow me because of it. – hevele Aug 20 '13 at 14:27
  • 1
    @MertToka then why don't instatiate it like: BitmapSource img = BitmapFrame.Create(uri,BitmapCreateOptions.None,BitmapCacheOption.OnLoad);. Maybe in the caller function and not here would be better. – sara Aug 20 '13 at 14:34
  • @sara But I don't get the image from uri, it get it from webcam. When I write BitmapSource img = BitmapFrame.Create(bitmap,BitmapCreateOptions.None,BitmapCacheOption.OnLoad); it says that best overloaded function does not take bitmap as parameter. – hevele Aug 20 '13 at 14:51

2 Answers2

-1

I would suggest making the filename something more generic so appending a timestamp or something, but additionally, in the using call fstream.Flush() before fstream.Close()

ars265
  • 1,949
  • 3
  • 21
  • 37
-1

Do you reference that file location (Mainly the Photos folder) anywhere else in your code before it reaches this point?

It seems like you have accessed this elsewhere in your code and the connection to it has not been closed off - it's handy to use the Using block whenever calling IO methods.

Although it's not ideal, try to call GC.Collect() after encoder.Rotation = Rotation.Rotate270;

Aaron
  • 86
  • 1
  • 5
  • I just reach the folder to add the image as an email. But it is later in the code. – hevele Aug 20 '13 at 13:46
  • Will the file that you are trying to access always have to be in the "Debug" folder? Because that might be the problem, in that DLLs etc are stored in this folder which are access by the compiler. If the files were stored in a temp folder, then if you changed the 'FileStream' parameters to point to the temp folder - that might help? Not entirely sure but it might work! – Aaron Aug 20 '13 at 14:49
  • I normally use Release folder, but it was giving the same error. In order to find problem, I switched to debug version. – hevele Aug 20 '13 at 14:53