C# - Unable to display image in picturebox -


working on project (or do) following:

  • access images in folder (for test phase using folder in appdata called 'test images')
  • place them in new folder, image name used place in or create sub-folder using algorithm
  • save images in 'list' when have been saved, can displayed in picturebox on application - can cycled through using prev/next buttons

the first 2 steps work fine, , have checked images (only using 2 image files test phase) have been placed in subfolders.

i initialised array of strings using directory.getallfiles() function in order this, meaning array contains paths of files being copied , manipulated. part of process, added each file path list<bitmap> able displayed in picturebox.

however, whenever try run function display image in picturebox, whole application crashes. no exception thrown or anything, whole thing stops working. have no idea going on.

i've tried using picbox.image = element on list<bitmap> or using picbox.imagelocation = it's file path, neither has been successful. code should make happen shown below:

    public void savelatestimages()     {         //specific path pictures         string testimagespath = environment.getfolderpath(environment.specialfolder.applicationdata) + "\\test images";         //if there pictures folder         if (directory.exists(testimagespath))         {                //get number of files in folder             int filecount = directory.getfiles(testimagespath).count();              //more 1 file in folder             if (filecount > 0)             {                    //create data structures store file info                 //filepaths holds path of each file represented string                 string[] filepaths = directory.getfiles(environment.getfolderpath(environment.specialfolder.applicationdata) + "\\test images");                  //for each file in pictures...                 (int index = 0; index < filecount; ++index)                 {                     //get name of image @ current index                     imagename = filepaths[index];                     //separate part relating patient name (everything before (dd/mm/yyyy))                     string subspecifier = imagename.split('\\').last();                     subspecifier = subspecifier.split('_')[0];                     //add root directory form subfolder name                     subdirectory = path.combine(rootdirectory, subspecifier);                      //subdirectory name formulated, check pre-existing                     //subfolder not exist                     if(!directory.exists(subdirectory))                     {                         //create                         directory.createdirectory(subdirectory);                     }                     //otherwise, file added existing directory                      //take end , folder\file division unique filename                     string filename = imagename.split('\\').last();                     //add existing subdirectory                     filename = path.combine(subdirectory, filename);                      //copy image subfolder using unique filename                     file.copy(imagename, filename, true); //true gives instruction overwrite existing file same path                      //add full filename list of bitmap images                     images.add(new bitmap(filename));                      //update shortcut file in image storage shortcut folder                     shortcutdirectory = getshortcut(subspecifier, filename);                 }             }         }     }       public void displaylatestimages()     {         //there images in list         if (images.count > 0)         {             //picbox defaults first image             picboximage.imagelocation = path.getfilename(images.first().tostring());         }          //check images before or after current image enable buttons         checkfirstlast();     } 

this shows saves images new file location, , should allow first image added list displayed.

any advice on how correct appreciated!

thanks, mark


Comments