Error while trying to save camera captured image on sd card

I am using following code to save a camera picture ion sd card :


 void ProgramStarted()
        {
            sdCard.MountSDCard();

            camera.PictureCaptured += new Camera.PictureCapturedEventHandler(camera_PictureCaptured);
            button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
       }
 
        void button_ButtonPressed(Button sender, Button.ButtonState state)
        {
            camera.TakePicture();
        }

        void camera_PictureCaptured(Camera sender, GT.Picture picture)
        {

            if (VerifySDCard())
            {
                string path = sdCard.GetStorageDevice().RootDirectory;
                byte[] bytes = new byte[picture.MakeBitmap().Width * picture.MakeBitmap().Height * 3 + 54];
                try
                {
                    sdCard.GetStorageDevice().WriteFile(path +"\\"+ (DateTime.Now.ToString()).ToString() + ".bmp", bytes);
                    Debug.Print("Saved");
                }
                catch (Exception ex)
                {
                    Debug.Print("Message: " + ex.Message + "  Inner Exception: " + ex.InnerException);
                }
            }
        }
        

        bool VerifySDCard()
        {
            if (!sdCard.IsCardInserted || !sdCard.IsCardMounted)
            {
                System.Threading.Thread.Sleep(2000);
                return false;
            }

            return true;
        } 
```cs][/code


but I get the following error :

A first chance exception of type 'System.ArgumentException' occurred in System.IO.dll

any helps !

In Visual Studio, set a break point on methods camera_PictureCaptured and VerifySDCard. Start debugging and see where it fails.

@ danibjor -

believe me I have done that ! and that’s the bug I get !

@ karamafrooz - On what line does it fail?

most likely the format of the time string contains an invalid character for a file name. i believe it contains a colon, which is invalid.

@ Mike - Good point there.

Try using

@ Mike -

I tried :

sdCard.GetStorageDevice().WriteFile(path + @ “\1.bmp”, bytes);

but I get this error :

#### System.IO.FileStream::.ctor [IP: 00d1] ####
#### System.IO.File::WriteAllBytes [IP: 0012] ####
#### Gadgeteer.StorageDevice::WriteFile [IP: 0012] ####
#### SDCardTest.Program::camera_PictureCaptured [IP: 0044] ####
#### Gadgeteer.Modules.GHIElectronics.Camera::OnPictureCapturedEvent [IP: 0037] ####
#### System.Reflection.MethodBase::Invoke [IP: 0000] ####
#### Gadgeteer.Program::DoOperation [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 0054] ####
#### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
#### Gadgeteer.Program::Run [IP: 0020] ####

The thread ‘’ (0x3) has exited with code 0 (0x0).
A first chance exception of type ‘System.IO.IOException’ occurred in Microsoft.SPOT.IO.dll
A first chance exception of type ‘System.IO.IOException’ occurred in System.IO.dll
Message: Exception was thrown: System.IO.IOException Inner Exception:

@ danibjor -

at this line :

sdCard.GetStorageDevice().WriteFile(path +"\"+ (DateTime.Now.ToString())

@ karamafrooz - yes, to:

sdCard.GetStorageDevice().WriteFile(path +"\"+ (DateTime.Now.ToString(“yyyyMMdd_HHmmss”))

DateTime.Now.ToString() returns at time format like 8:23, and you can’t have a colon in a file name.

The guys have pinpointed your issue but I wanted to give another suggestion to help your diagnosing this kind of issue in the future.

A simple debug step in a case like this where you fail on a known particular line is to break apart the compound instruction. Create a string that is the ToString output, and inspect it. Also use a fixed string of some other form. You would have seen the character in what becomes the output filename and seen that your code works when using a simpler name, and you could have come to the conclusion that the name was wrong.

I also subscribe to the additive approach - the “fundamental” problems in this area of code are writing to a file and taking the picture. Ensure you know how to achieve both independently, then bring them together. That way you know that as each new issue comes up you can step back to the fundamentals that you have already addressed and in this case you should be able to pick up how your code change for the filename was what generated the fault