Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot save file - already in use #88

Open
MikeDuf opened this issue Aug 23, 2021 · 1 comment
Open

Cannot save file - already in use #88

MikeDuf opened this issue Aug 23, 2021 · 1 comment

Comments

@MikeDuf
Copy link

MikeDuf commented Aug 23, 2021

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants