0

whenever I am trying to save the webcamera capture image automatically then then runtime error is coming in path.automaticaly name like 0.jpg,02.jpg,03.jpg like this way image will save in the particular mention folder.but giving run time error. plz check this.

namespace camera1
{
    public partial class Form1 : Form
    {
        private Capture capture;
        private bool captureinprogress;

        public Form1()
        {
            InitializeComponent();
        }
        private void ProcessFrame(object sender, EventArgs arg)
        {
           Image<Bgr, Byte> ImageFrame = capture.QueryFrame();
           cameraimage.Image = ImageFrame;
           string root = "C:\\photo\0"; // automatically saving image to c drive like       001.jpg,002.jpg;
           for (int i = 0; i < 100; i++)
           {
               if (File.Exists(" "))
               { }
               else
               {
                   string Path = root + i + ".jpg";
                   ImageFrame.Save(Path);
               }

               {
                   if (ImageFrame != null)
                   {
                       pictureBox1.Image = ImageFrame.ToBitmap();
                   }
                   if (pictureBox1 != null)
                   {
                       pictureBox2.Image = ImageFrame.ToBitmap();
                   }
                   if (pictureBox2 != null)
                   {
                       pictureBox3.Image = ImageFrame.ToBitmap();
                   }
             }
         }
     }
     private void btnStart_Click(object sender, EventArgs e)
     {
        if (capture == null)
        {
            try
            {
                capture = new Capture();
            }
            catch (NullReferenceException excpt)
            {
                MessageBox.Show(excpt.Message);
            }
        }
        if (capture != null)
        {
            if (captureinprogress)
            {  //if camera is getting frames then stop the capture and set button Text
                // "Start" for resuming capture
                btnstart.Text = "Start!"; //
                Application.Idle -= ProcessFrame;
            }
            else
            {
                //if camera is NOT getting frames then start the capture and set button
                // Text to "Stop" for pausing capture
                btnstart.Text = "Stop";
                Application.Idle += ProcessFrame;
            }

            captureinprogress = !captureinprogress;        
        }
    }

    private void ReleaseData()
    {
        if (capture != null)
            capture.Dispose();
    }

}

}
Marco
  • 22,856
  • 9
  • 75
  • 124
Nirupam
  • 24
  • 1
  • 5

2 Answers2

1

You need to mask the backslashes, or the compiler will try to interpret \p oder \0, which he can't

So the easiest way to accomplish this is to add an @ at the start of your string.

string root = @"C:\photo\0";

Or you use double-backslashes all the time:

string root = "C:\\photo\\0";

For further reference, please read:

Community
  • 1
  • 1
Marco
  • 22,856
  • 9
  • 75
  • 124
0

I dont have a instance here but you need to change this to

string root = @"C:\photo\0\";// instead of string root = "C:\\photo\0";

note: You cannot save directly into C: Drive because you need to run the program with administrative privileges. I suggest using a folder on desktop or some library folder such as My Pictures.

Flood Gravemind
  • 3,773
  • 12
  • 47
  • 79
  • i tried. ImageFrame.Save(Path); still error coming on this line – Nirupam Oct 20 '13 at 12:15
  • @Nirupum just as I suspected Serv and Me we were both wrong and my first answer was correct. You just need to add an end '\' so you get the folder '@"C:\photo\0\"' instead of file. – Flood Gravemind Oct 20 '13 at 12:40
  • sir,i tried with slash but giving the same nullreference exception in the line ImageFrame.save(path); – Nirupam Oct 20 '13 at 16:44
  • @Nirupam Does the Folder '@"C:\photo\0\"' exist on the disk? Also note that you cannot save directly into C: Drive because you need to run the program with administrative privileges. I suggest using a folder on desktop or some library folder such as My Pictures. – Flood Gravemind Oct 21 '13 at 00:24
  • '@"C:\photo\0\"'--its giving error like cannot implicitly convert type char to string,too many character in character literal,unrecognized escape sequence.. – Nirupam Oct 21 '13 at 03:54
  • i tried to do in @"D:\photo\0\".same null reference error.once i tried to save in c drive.it save 6 image starting from 0010 to 0060.jpg like.then i start the project again and then again null referece exceptiob is coming. – Nirupam Oct 21 '13 at 03:57