oozcitak / exiflibrary

A .Net Standard library for editing Exif metadata
MIT License
131 stars 48 forks source link

Cannot save file - already in use #88

Open MikeDuf opened 3 years ago

MikeDuf commented 3 years ago

Hi, I am fairly new at coding and I am still learning. But this is something I really cannot find the answer for and I thought maybe it could be something you guys could pin point the issue?

Here is my code chunk. It always return "another process is using the file - cannot save" when I hit the save button of my interface. Any idea why? I tried to hard code it and it seems to work when ran in a command window but not in WPF (could it be when I load the file into a bitmap that it gets stuck in use?)

Thank you

        private void GetImage(string path)
        {
            string[] Files = Directory.GetFiles(path,"*.jpg");
            int nmb_Files = Files.Length;
            Random rand = new Random();
            int index = rand.Next(nmb_Files);
            string FileName = Path.GetFileNameWithoutExtension(Files[index]);
            TB_FileName.Text = FileName;
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            imgSource = path + "/" + FileName + ".jpg";
            bi.UriSource = new Uri(imgSource);
            bi.EndInit();
            img_Dessin.Source = bi;
            FindTags(imgSource);
        }

        private void FindTags(string imageSource)
        {
            var file = ImageFile.FromFile(imageSource);
            string tags;
            if (file.Properties.Get<WindowsByteString>(ExifTag.WindowsKeywords) !=null)
            {
                tags = file.Properties.Get<WindowsByteString>(ExifTag.WindowsKeywords);
            }
            else
            {
                tags = "";
            }
            TB_Tags.Text = tags;
        }

        private void SaveTags()
        {
            var file = ImageFile.FromFile(imgSource);
            string tagsToAdd = TB_Tags.Text;
            file.Properties.Set(ExifTag.WindowsKeywords, tagsToAdd);
            try
            {
                file.Save(imgSource);
            }
            catch (Exception)
            {
                MessageBox.Show("Impossible d'enregistrer", "Fichier deja utilise");
            }
        }
RudyTheDev commented 2 years ago

I believe this is an issue not related to the project itself. Your access violation error is more than likely because img_Dessin.Source has a lock on the file. This is because you are not actually loading the image yourself and storing it in memory, but rather letting WPF stuff handle it and WPF locks it because it's working directly with the file. See something like https://stackoverflow.com/questions/29097171/sharing-violation-on-image-view-and-edit-wpf