Lara
Lara

Reputation: 3174

How to get an image from a file, and overwrite it in the same program?

(Link to code: http://pastebin.com/0CHKnm6W)

My problem is as follows:

The program contains a function to import an image (this is under part 2). The path to the image is then saved in a class which contains the image.

The program contains a second function which takes the image as you edited it using the program, and then saves it to the original path (this being the original path matters quite a bit)

This gives an exception (IOException) because the path we want to save to contains an image that we are using. How can I fix this?

Upvotes: 1

Views: 79

Answers (1)

Sinatr
Sinatr

Reputation: 21998

From Image.FromFile

The file remains locked until the Image is disposed.

You could could do it this way

using(var image = Image.FromFile(openFileDialog.FileName))
{
    ...
    s.schetscontrol.MaakBitmapGraphics().DrawImageUnscaled(image, 0, 0);
    ...
}

Maybe cloning will works too.

Please consider next time provide SSCCE right here, hosting it elsewhere and in some strange language is not helpful at all when you need help. If you would spend 5 min formatting your sample, you'd get answer 1 hour earlier ;)

Upvotes: 1

Related Questions