I have a little Game that works with Pixels. I tried to simplify this error but I can't, so it would be great if someone knew a solution. I have a Loadlevel function that deletes some state files and works with image, saving some informations etc..
public void LoadLevel(string Path)
{
bool done = false;
int i = 0;
while (!done)
{
if (File.Exists(path + @"\Game\State\" + i + ".png"))
{
File.Delete(path + @"\Game\State\" + i + ".png");
i++;
}
else
{
done = true;
}
}
image = new Bitmap(Path);
current = 0;
Screen.Source = new BitmapImage(new Uri(path + @"\Game\Levels\Level " + Level + ".png"));
//Then just some checks and saving
}
I have another function that is called everytime my Player moves and it saves the current state with a new name to a folder and sets the Image.Source as the new Image.
public void Draw()
{
image.SetPixel(pos.x, pos.y, System.Drawing.Color.FromArgb(0, 255, 255));
image.Save(path + @"\Game\State\" + current + ".png");
Screen.Source = new BitmapImage(new Uri(path + @"\Game\State\" + current + ".png"));
current++;
}
(Loadlevel gets called when the Finish is reached)
The problem is that in Loadlevel at File.Delete(path + @"\Game\State\" + i + ".png");
I get a System.IO.IOException: the process cannot access the file: path
The Problem lies in the Screen.Source since when i comment it out everything works just fine. Does someone know how to fix this problem?