6 Replies - 9258 Views - Last Post: 27 June 2011 - 09:49 AM Rate Topic: -----

#1 sweetasman01  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 10-April 11

C# Slide Show, how to add photos?

Posted 25 June 2011 - 10:07 AM

Hi,

I am trying to make a slide show application in C#. So far I have it working, but what I am having trouble with is when a new photo is dropped into the selected directory. The photo isnt picked up and added to the the sideshow list. The application is designed to be left alone all day and the end user doesn't want to restart the application each time they add a photo. What ideally should happen is the when they add a photo, the slide show continues to the end and then restarts from the beginning but this time includes the photo that was added.

I have tried to set the timer to recheck the images in the directory each time the images reach the end of sildeshow but am not having much luck.

Any suggestions?

My code is below.

Thanks

Carl



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PullManSlideShow
{
    public partial class Form1 : Form
    {
        int fileMax = 0;
        int fileCount = 0;

        public Form1()
        {
            InitializeComponent();
        }

        string[] pictureLocationBoxArray = new string[100];
                
        FolderBrowserDialog pictureSelection = new FolderBrowserDialog();
       

        public void ImageIdentification(string a) //passes in all the locations of the files
        {
            //checks to see if is suitable type
            if ((a.EndsWith(".jpg")) || (a.EndsWith(".png")) || (a.EndsWith(".gif")) || (a.EndsWith(".jpeg")) || (a.EndsWith(".bmp"))
                || (a.EndsWith(".JPG")) ||(a.EndsWith(".JPEG")) || (a.EndsWith(".GIF")) || (a.EndsWith(".PNG")) || (a.EndsWith(".BMP")))
            {              
                pictureLocationBoxArray[fileMax] = a;//stores location of images
                //MessageBox.Show(a);
                
                Image im = Image.FromFile(a);
                PictureBox pb = new PictureBox();
                pb.Image = im;
                panel1.Controls.Add(pb);
                fileMax++;
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (fileCount == fileMax)
            {
                fileCount = 0;
                ////////////////////////
                // this is possible solution but doesnt seem to work

                string tempString = "";
                int stringLength = 0;

                tempString = pictureLocationBoxArray[fileCount];

                stringLength = tempString.Length;
                
                tempString = tempString.Substring(0, (stringLength - 8));
                // -8 becuase 000.jpeg is 8 letters, possible peoblem if 000.gif etc
                // Images are labelled like 000.jpeg where 0s are numbers. eg 001.jpeg is the first images shown, 019.jpeg is
                // second. If a new image is added say 005.jpeg it will now come second on the list 001.jpeg,005.jpeg,019,jpeg etc
               
                // Get updated list of files
                //string[] images = Directory.GetFiles(tempString);
                panel1.Controls.Clear();

                foreach (string a in Directory.GetFiles(tempString))
                {
                    ImageIdentification(a);
                }

            }
            PictureBox p = (PictureBox) panel1.Controls[fileCount];
            pictureBox1.Image = p.Image;
            fileCount++;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            fileMax = 0;
            //panel1.Controls.Clear();
            if (pictureSelection.ShowDialog() == DialogResult.OK)
            {
                foreach (string a in Directory.GetFiles(pictureSelection.SelectedPath))
                {                  
                    ImageIdentification(a);
                }

                timer1.Enabled = true;
            }
        }
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: C# Slide Show, how to add photos?

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9574
  • View blog
  • Posts: 36,273
  • Joined: 12-June 08

Re: C# Slide Show, how to add photos?

Posted 25 June 2011 - 10:12 AM

Have you looked into the 'file system watcher'? It's pretty much geared towards what you want. It throws an event when a directory or file has changed.

Tutorial on it:
http://www.dreaminco...mwatcher-class/

MSDN doc
http://msdn.microsof...temwatcher.aspx
Was This Post Helpful? 0
  • +
  • -

#3 sweetasman01  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 10-April 11

Re: C# Slide Show, how to add photos?

Posted 25 June 2011 - 10:38 AM

Thanks I will look at that now. I only started coding a few months ago, so all this stuff is brand new to me.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9574
  • View blog
  • Posts: 36,273
  • Joined: 12-June 08

Re: C# Slide Show, how to add photos?

Posted 25 June 2011 - 10:39 AM

No problem. The tutorial should go a long way to getting you the knowledge base to run with it.
Was This Post Helpful? 1
  • +
  • -

#5 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: C# Slide Show, how to add photos?

Posted 27 June 2011 - 05:35 AM

What problems were you having with your original approach? One thing I noticed was that you reset fileCount to 0, but not fileMax.
Was This Post Helpful? 1
  • +
  • -

#6 sweetasman01  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 10-April 11

Re: C# Slide Show, how to add photos?

Posted 27 June 2011 - 08:56 AM

I had it working but when a new photo was added to the pics folder it wasnt picking it up, so I added some code to the timer below the ///////////. Now it runs once then I get an out of bounds error on an array, even if I dont add any photos. So what you are saying might fix the problem.

edit: Ok so it worked!!! OMG it was so simple!!!

Here is the updated code:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PullManSlideShow
{
    public partial class Form1 : Form
    {
        int fileMax = 0;
        int fileCount = 0;

        public Form1()
        {
            InitializeComponent();
        }

        string[] pictureLocationArray = new string[100];
                
        FolderBrowserDialog pictureSelection = new FolderBrowserDialog();
       

        public void ImageIdentification(string a) //passes in all the locations of the files
        {
            //checks to see if is suitable type
            if ((a.EndsWith(".jpg")) || (a.EndsWith(".png")) || (a.EndsWith(".gif")) || (a.EndsWith(".jpeg")) || (a.EndsWith(".bmp"))
                || (a.EndsWith(".JPG")) ||(a.EndsWith(".JPEG")) || (a.EndsWith(".GIF")) || (a.EndsWith(".PNG")) || (a.EndsWith(".BMP")))
            {              
                pictureLocationArray[fileMax] = a;//stores location of images
                //MessageBox.Show(a);
                
                Image im = Image.FromFile(a);
                PictureBox pb = new PictureBox();
                pb.Image = im;
                panel1.Controls.Add(pb);
                fileMax++;
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (fileCount == fileMax)
            {
                fileCount = 0;
                fileMax = 0;

                Path.GetDirectoryName(pictureLocationArray[fileCount]);

                panel1.Controls.Clear();

                foreach (string a in Directory.GetFiles(Path.GetDirectoryName(pictureLocationArray[fileCount])))
                {
                    ImageIdentification(a);
                }

              (PictureBox)panel1.Controls[fileCount];
           }
           
            
                PictureBox p = (PictureBox)panel1.Controls[fileCount];
                pictureBox1.Image = p.Image;
                fileCount++;
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            fileMax = 0;
            if (pictureSelection.ShowDialog() == DialogResult.OK)
            {
                foreach (string a in Directory.GetFiles(pictureSelection.SelectedPath))
                {                  
                    ImageIdentification(a);
                }

                timer1.Enabled = true;
            }
        }
    }
}




I was able to combine about five lines of code into one with the path library, which I am quite happy with.

Thank you so much.

This post has been edited by sweetasman01: 27 June 2011 - 09:04 AM

Was This Post Helpful? 1
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9574
  • View blog
  • Posts: 36,273
  • Joined: 12-June 08

Re: C# Slide Show, how to add photos?

Posted 27 June 2011 - 09:49 AM

good job!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1