0

I'm developing a program which will capture images four times and save it into a folder

what I did was to put my codes inside a for loop

but my problem is it only saves the first captured image four times instead of capturing four times

private void Form1_Load(object sender, EventArgs e)
{
    string path = @"C:\Users\\Jake_PC\\Desktop\\fitting\\";
    System.IO.Directory.CreateDirectory(path);
    bool useCam = true;

    if (!useCam)
        measureImage(null);
    else 
    {
        try
        {
            camera = new Capture();
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.Message);
            return;
        }

        Application.Idle += viewImage;
        captureProcess = true;
    }
}

private void btnCapture_Click(object sender, EventArgs e)
{
    if (btnCapture.Text == "Back")
    {
        Application.Restart();
    }
    else
    {
        if (captureProcess == true)
        {
            for (int cap = 0; cap < 4; cap++)
            {
                string path = @"C:\\Users\\Jake_PC\\Desktop\\fitting\\";
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
                ctr = dir.GetFiles().Length;

                camera = new Capture();
                System.Threading.Thread.Sleep(500);
                Application.Idle -= viewImage;
                SaveFileDialog dlg = new SaveFileDialog();
                img.ToBitmap().Save(@"C:\\Users\\Jake_PC\\Desktop\\fitting\\" + ctr + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                camera.Dispose();

                Form1_Load(sender, e);
            }
        }
    }
}
newbie07
  • 83
  • 3
  • 16
  • 3
    That code is a mess of calling functions back and forth without clearly defined concerns and boundaries. Why don't you make one function that captures and saves an image and nothing else. Totally independent of any user interface. It is a lot easier to find errors in well defined environments. – nvoigt Feb 04 '14 at 11:25
  • What is the fps of your camera?If it's around 30fps then the difference in successive images will be hardly noticeable. – Naren Feb 04 '14 at 12:44
  • @Naren I've edited the code and tried to put threading to put some delays on the capture but still the same result – newbie07 Feb 04 '14 at 15:35
  • 1
    @newbie07 What is 'img'? Is it a global variable?Please post the whole program to find the error. – Naren Feb 05 '14 at 04:58

1 Answers1

0

Use the following base as your code http://www.emgu.com/wiki/index.php?title=Camera_Capture

then replace the following method ProcessFrame(object sender, EventArgs arg) with this code:

    int count = 0;
    static string path = @"C:\\Users\\Jake_PC\\Desktop\\fitting\\";
    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);

    private void ProcessFrame(object sender, EventArgs arg)
    {
        //***If you want to access the image data the use the following method call***/
        //Image<Bgr, Byte> frame = new Image<Bgr,byte>(_capture.RetrieveBgrFrame().ToBitmap());


        string ctr = dir.GetFiles().Length.ToString();

        if (RetrieveBgrFrame.Checked)
        {
            Image<Bgr, Byte> frame = _capture.RetrieveBgrFrame();
            //because we are using an autosize picturebox we need to do a thread safe update
            DisplayImage(frame.ToBitmap());
            frame.ToBitmap().Save(@"C:\\Users\\Jake_PC\\Desktop\\fitting\\" + ctr + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            count++;
            if(count < 4)
            {
                //this should really be reset in captureButtonClick() method 
                //to prevent a frameready event fireing before disposeing is finsihed
                count = 0; 
                //Stop aquiring by simulating a form button press
                captureButtonClick(null, null); 
            }
        }
        else if (RetrieveGrayFrame.Checked)
        {
            Image<Gray, Byte> frame = _capture.RetrieveGrayFrame();
            //because we are using an autosize picturebox we need to do a thread safe update
            DisplayImage(frame.ToBitmap());
            frame.ToBitmap().Save(@"C:\\Users\\Jake_PC\\Desktop\\fitting\\" + ctr + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            count++;
            if (count < 4)
            {
                //this should really be reset in captureButtonClick() method 
                //to prevent a frameready event fireing before disposeing is finsihed
                count = 0;
                //Stop aquiring by simulating a form button press
                captureButtonClick(null, null);
            }
        }
    }

let me know if you have issues,

Cheers,

Chris

Chris
  • 3,462
  • 20
  • 32