Save image from camera to SD card

First time using camera, and trying to save image to sd card
There is no documentation for the Bitmap, or Camera or any other methods :cry:

I get ArgumentException

#### Exception System.ArgumentException - 0xfd000000 (1) ####
#### Message: 
#### System.Drawing.Internal.Bitmap::.ctor [IP: 0000] ####
#### System.Drawing.Graphics::CreateSurface [IP: 000c] ####
#### System.Drawing.Graphics::.ctor [IP: 0006] ####
#### System.Drawing.Bitmap::.ctor [IP: 0014] ####
#### CameraTest.Program::Main [IP: 00a7] ####
Exception thrown: 'System.ArgumentException' in GHIElectronics.TinyCLR.Drawing.dll
An unhandled exception of type 'System.ArgumentException' occurred in GHIElectronics.TinyCLR.Drawing.dll


class Program
{
   
    static void Main()
    {
        var controller = I2cController.FromName(SC20260.I2cBus.I2c1);

        // Camera
        var ov9655 = new Ov9655Controller(controller);
        var id = ov9655.ReadId();

        Debug.WriteLine("id = " + id);

        var ptr = Memory.UnmanagedMemory.Allocate(640 * 480 * 2);
        var data = Memory.UnmanagedMemory.ToBytes(ptr, 640 * 480 * 2);

        var sd = StorageController.FromName(SC20260.StorageController.SdCard);
        var drive = FileSystem.Mount(sd.Hdc);

        var directory = new DirectoryInfo(drive.Name);
        Debug.WriteLine(directory.FullName);
        var files = directory.GetFiles();

        ov9655.SetResolution(Ov9655Controller.Resolution.Vga);

        foreach (var f in files)
        {
            System.Diagnostics.Debug.WriteLine(f.Name);
        }

        while (true)
        {
            ov9655.Capture(data, 100);

            var b1 = new Bitmap(data, BitmapImageType.Jpeg);

            var bb = b1.GetBitmap();

            var file = new FileStream($@"{drive.Name}{DateTime.Now.Ticks}.jpg", FileMode.OpenOrCreate);

            file.Write(bb, 0, data.Length);

            file.Flush();

            Thread.Sleep(5000);
        }
    }
}
1 Like

try to add try catch when Capture(data, 100).

Also, the image data is in 16bpp raw binary. If you want to show the picture in PC you need convert them to bmp format which need to add header only, or use our support function below:

 var bm = new Bitmap(data, width, height);
 bm.Save(yourFilestream, System.Drawing.Imaging.ImageFormat.Bmp);

If you want jpg, TinyCLR doesn’t support converting from bmp to jpg, and we don’t recommend them in C#.

1 Like

Thanks, its working, BMP is fine.
Its a start.