Search code examples
c#access-denied

Editting the picturebox image using external editor and loading the new image


I try to open an external editor during Runtime and edit an image which is currently is set to a PictureBox, edit the image and update the image after closing the editor.
For this, I have a simple c# windows application with a PictureBox two Button one to load PictureBox Image from file and another to edit the image using MS Paint.
Here is the code:

public partial class Form1 : Form
{
    string myImagePath = @"C:\temp\bt_logo.png";
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        pictureBox1.Image = Image.FromFile(myImagePath);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        System.Diagnostics.ProcessStartInfo launchEditor = new System.Diagnostics.ProcessStartInfo();
        launchEditor.FileName = "mspaint";
        launchEditor.Arguments = myImagePath;
        launchEditor.UseShellExecute = true;
        System.Diagnostics.Process.Start(launchEditor);
    }
}

The Editor is openned successfully but the changes cannot be saved because of access problem:

enter image description here

Any idea how to solve this problem?

EDIT1:
In order to stop accessing the image when the editor is open, I modified the code of button2_Click() as follows:

pictureBox1.Image = null;
System.Diagnostics.ProcessStartInfo launchEditor = new System.Diagnostics.ProcessStartInfo();
launchEditor.FileName = "mspaint";
launchEditor.Arguments = myImagePath;
launchEditor.UseShellExecute = true;
System.Diagnostics.Process.Start(launchEditor);
pictureBox1.Image = Image.FromFile(myImagePath);

same resault. As another try, I made a copy of the image, modified it and copied it to the original image,

System.IO.File.Copy(myImagePath, myImagePath_temp, true);
System.Diagnostics.ProcessStartInfo launchEditor = new System.Diagnostics.ProcessStartInfo();
launchEditor.FileName = "mspaint";
launchEditor.Arguments = myImagePath_temp;
launchEditor.UseShellExecute = true;
System.Diagnostics.Process.Start(launchEditor);
System.IO.File.Copy(myImagePath_temp, myImagePath, true);

the same resault!


Solution

  • This question is essentially a duplicate of

    Open Image from file, then release lock?

    Image.FromFile locks the file on disk, until the image variable in your code is disposed of. You should observe the accepted answer to that question above; it provides a way to load the image data from disk, copy it inside the memory of your program, and then release the lock on the file and use the copy of the bitmap data in your picturebox

    You'll find an answer on how to load your image without it being locked in the file system. I'm writing an answer instead of a comment because I wanted to give a bit of further advice to the rest of the program

    Take a look at the FileSystemWatcher class; Notification when a file changes?

    you can use it to detect changes to your file so the picture box can be updated when the file is saved rather than when paint is quit- this will be easier than coding to detect the app closing