0

So I am trying to make a method that will create PictureBoxes as need be, and the specific image that I am trying to load onto the Image is flying the error;

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in System.Drawing.dll

Additional information:

\students\student\30232634\My Documents\Visual Studio 2015\Projects\WindowsFormsApplication4\WindowsFormsApplication4\bin\Debug/Graphics/enemyAlien.png

The Code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

namespace WindowsFormsApplication4
{
    class Aliens : PictureBox
    {
        public string alienImage = "Enemy.png";
        public int alienCount = 30;
        public int posX { get; set; }
        public int posY { get; set; }
        public Aliens AlienSpawn()
        {
            Aliens picture = new Aliens();
            picture.Name = "pictureBox";
            picture.Size = new Size(64, 64);
            picture.Location = new Point(100, 100);
            picture.Image =Image.FromFile(Directory.GetCurrentDirectory()+"/Graphics/enemyAlien.png");
            return picture;
        }
    }
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • So you're positive that the file is in `\students\student\30232634\My Documents\Visual Studio 2015\Projects\WindowsFormsApplication4\WindowsFormsApplication4\bin\Debug/Graphics/enemyAlien.png`? – Federico klez Culloca Feb 07 '19 at 10:30
  • Take care of the urls: you are mixing `/` and `\\` so i think that you should fix your path Image... – R. García Feb 07 '19 at 10:31
  • What is the exact value of `Directory.GetCurrentDirectory()+"/Graphics/enemyAlien.png` (don't guess)? – mjwills Feb 07 '19 at 10:37
  • Possible duplicate of [Best way to get application folder path](https://stackoverflow.com/questions/6041332/best-way-to-get-application-folder-path) – mjwills Feb 07 '19 at 10:38

0 Answers0