0

I have a DataGrid in my wpf window, and it's got a ContextMenu with items Update and Delete

When Update is Clicked the selected item's Name, UserName etc... is shown in TextBoxes of the window, so that the user can change them (when the user clicks on a addUserBtn the changes are saved to database)

Now, one of the User's properties is thier Image, and i show that image using Image element in WPF. the source of the image is by default a file inside the project. but when Update is clicked, the ImageSource becomes the User's Image's address (gotten from database)

I've used the code below to Copy the image file into the application's executable path, and then save the file's address in the database

string SavePic(string UserName)
        {
            string path = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) + @"\UserPictures\";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            string picName = UserName + ".jpg";
            try
            {
                string picPath = ofd.FileName;
                File.Copy(picPath, path + picName, true); 
            }
            catch (Exception e)
            {
                MyMessageBox mb = new MyMessageBox("System was unable to save the picture\n" + e.Message);
                mb.ShowDialog();
            }
            return path + picName;
        }

So far so good

But the problem is when i try to save the changes (after clicking on UpdateContextMenu).

Weather I change the image or not, i get an error

Let's say I only want to change the User's Name, but the image is untouched, then I get this error .Empty file name is not legal Parameter name: sourceFileName

And if I change the image, I get this other error => The process cannot access the file 'C:Usersetc....because it is being used by another process

Just in case, this is the code, behind UpdateContextMenu button:

flagUpdate = true;
            User temp = bll.Read(idValue);
            txtName.Text = temp.Name;
            txtUsername.Text = temp.UserName;
            if (temp.Picture != null)
            {
                ProfilePicImage.Source = new BitmapImage(new Uri(temp.Picture));
            }
            else
            {
                ProfilePicImage.Source = new BitmapImage(new Uri("pack://application:,,,/Image/UserWindow/UserProfileImage.png"));
            }

And this is the code behind addUserBtn which saves the new values to the database:

User tempId = bll.Read(idValue);
                        User temp = new User();
                        temp.Name = txtName.Text;
                        temp.UserName = txtUsername.Text;
                        temp.Picture = SavePic(txtUsername.Text);
                        if (txtPassword.Text != "")
                        {
                            temp.Password = txtPassword.Text;
                        }
                        string action = bll.Update(temp, idValue);
                        ProfilePicImage.Source = new BitmapImage(new Uri("pack://application:,,,/Image/UserWindow/UserProfileImage.png"));
                        flagUpdate = false;
                        MyMessageBox mb = new MyMessageBox(action);
                        OpenWindow(mb);
                        DataGridFill();

it got very long :)

just wanted to give you all the information just in case, since I myself am clueless about what I should do

Mohammad
  • 98
  • 8
  • The reason for the error message is that BitmapImage by default keeps the source file open. See e.g. [here](https://stackoverflow.com/a/29625691/1136211) or [here](https://stackoverflow.com/a/13265190/1136211) for a solution. You should however avoid to "*copy the image file into the application's executable path*", because your application may not be allowed to write into that folder. – Clemens Aug 30 '22 at 05:24
  • I read the answers given in the two pages you recommended but since i'm fairly new to C# i can't quite understand what exactly i should do can you take a look at my SavePic method and tell me how i should write the code plz? – Mohammad Sep 02 '22 at 13:32

0 Answers0