title: Display images from directory
body: <p>I have the following code:</p>
  
  <pre><code>private void Form2_Load(object sender, EventArgs e)
      {
          string[] files = Directory.GetFiles(Form1.programdir + "\\card_images", "*", SearchOption.TopDirectoryOnly);
          MessageBox.Show(files.ElementAt(1).ToString());
          PictureBox[] cards = new PictureBox[files.Count()];
          for (int i = 0; i &lt; files.Count(); i++)
          {
              MessageBox.Show(i.ToString());
              cards[i] = new PictureBox();
              cards[i].BackgroundImage = new Bitmap(Form1.programdir + "\\card_images\\" + files[i]);
              MessageBox.Show(Form1.programdir + "\\card_images\\" + files[i]);
              cards[i].Padding = new Padding(0);
              cards[i].BackgroundImageLayout = ImageLayout.Stretch;
              cards[i].MouseDown += new MouseEventHandler(card_click);
              cards[i].Size = new Size((int)(this.ClientSize.Width / 2) - 15, images.Height);
                  images.Controls.Add(cards[i]);
          }
      }
  </code></pre>
  
  <p>Note:  "images" is a FlowLayoutPanel in my program's graphic designer. I use it to easily keep track of the images.</p>
  
  <p>I'm trying to get it to display images of a few poker cards, from the directory. The directory is correct, the folder is there, and previous uses of the folder have worked; Form1 is the first form of the program, and "programdir" is:</p>
  
  <pre><code>public static string programdir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
  </code></pre>
  
  <p>Messageboxes seem to display the correct diagnostic information (folder location, card file names, even files.Count() seems to be correct), but I can't get any files displayed in pictureboxes. Any advice?</p>
  
  <p>EDIT: Apparently, the line</p>
  
  <pre><code>MessageBox.Show(Form1.programdir + "\\card_images\\" + files[i]);
  </code></pre>
  
  <p>Does not run, ever, at all, even though </p>
  
  <pre><code>MessageBox.Show(files.ElementAt(1).ToString());
  </code></pre>
  
  <p>Does run. As well, </p>
  
  <pre><code>MessageBox.Show(i.ToString());
  </code></pre>
  
  <p>Only runs once, outputting "0" in a messagebox (since "i" is set to 0 initially), even though it should run 4 times, since the size of the array "files" is 4.</p>
  
  <p>EDIT 2: The new code, which runs flawlessly, is:</p>
  
  <pre><code>private void Form2_Load(object sender, EventArgs e)
      {
          Uri baseUri = new Uri(Form1.programdir + "\\card_images\\");
          string[] files = Directory.GetFiles(Form1.programdir + "\\card_images", "*", SearchOption.TopDirectoryOnly);
          MessageBox.Show(files.Count().ToString());
          PictureBox[] cards = new PictureBox[files.Count()];
          for (int i = 0; i &lt; files.Count(); i++)
          {
              Bitmap bmp = null;
              Uri completeUri = new Uri(baseUri, files[i]);
              try
              {
                  bmp = new Bitmap(completeUri.LocalPath);
              }
              catch (Exception exc)
              {
                  // remove this if you don't want to see the exception message
                  MessageBox.Show(exc.Message);
                  continue;
              }
              MessageBox.Show(i.ToString());
              cards[i] = new PictureBox();
              cards[i].BackgroundImage = bmp;
              MessageBox.Show(Form1.programdir + "\\card_images\\" + files[i]);
              cards[i].Padding = new Padding(0);
              cards[i].BackgroundImageLayout = ImageLayout.Stretch;
              cards[i].MouseDown += new MouseEventHandler(card_click);
              cards[i].Size = new Size((int)(this.ClientSize.Width / 2) - 15, images.Height);
              cards[i].Visible = true;
                  images.Controls.Add(cards[i]);
          }
      }
  </code></pre>
  
  <p>The try/catch block isn't mandatory because it doesn't catch any errors anymore, but I'm leading it just in case something else happens some time down the road. This code will grab all images from the folder (with the assumption that only image files are in the folder, which is a separate issue), display them, and bake you a cake all at once.</p>