0

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?

  • In the draw method right click on "path" and select "Go To Definition". You have more the one variable "path" and the one being used in draw method is not set properly. – jdweng Apr 28 '21 at 11:59
  • @jdweng it is the right one `path = AppDomain.CurrentDomain.BaseDirectory.Substring(0,AppDomain.CurrentDomain.BaseDirectory.IndexOf(@"\bin"));` or in short, the path to the projects files. Path = the paramtere path when loading a level – Bananenkris Apr 28 '21 at 12:02
  • It makes no sense at all to save and load image files each time something happens in the UI. If you want to continuously modify a bitmap, use the WritableBitmap class. – Clemens Apr 28 '21 at 12:04
  • @Clemens But don't I need to save the Bitmap to use it as a source? – Bananenkris Apr 28 '21 at 12:07
  • No, you would use the WritableBitmap as Source of the Image element. – Clemens Apr 28 '21 at 12:26
  • Then the you do not have credentials to read file. You want to save results on client machine. The code is going where the bin folder of the project is installed which may be in the server. When you make a connection you are running the server with the credentials that starting the service. – jdweng Apr 28 '21 at 12:28

0 Answers0