0

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!

Hille
  • 2,123
  • 22
  • 39
Behy
  • 483
  • 7
  • 23
  • Maybe load a temporary copy of the image into the picturebox and replace it with the updated image once it was saved in Paint? – Ian H. Nov 20 '17 at 08:11
  • Ian it's because image.fromfile locks the file until image variable is disposed – Caius Jard Nov 20 '17 at 08:16
  • @Caius: but after changes of Edit1, it does not still working – Behy Nov 20 '17 at 08:22
  • for your first try in Edit1, very likely mspaint is still loading and has not already tried to load the image when your program runs `pictureBox1.Image = Image.FromFile(myImagePath)`. Why the second try fails is quite a mistery to me. – Gian Paolo Nov 20 '17 at 09:20

1 Answers1

3

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

Caius Jard
  • 72,509
  • 5
  • 49
  • 80