Save picture captured by the camera on SD-Card

Hi,

I’m trying to save the picture captured by the camera on the SD card and cannot convert the GT.Picture picture into byte:
My code:
[

private void camera_pictureCaptured(Camera sender, GT.Picture picture)
        {
           
            GT.StorageDevice storage = sdCard.GetStorageDevice();
            //save captured picture on SD card
// Picture_Captured  global defined. Here byte is required. How to convert GT.Picture in bye?
            Picture_Captured = new Bitmap(picture, Bitmap.BitmapImageType.Bmp);
            // allocate buffer
           byte[] outputFile = new byte[Picture_Captured.Width * Picture_Captured.Height * 3 + 54];  //* 3 + 54 (3 colors)
            // convert to BMP file
            Util.BitmapToBMPFile(Picture_Captured.GetBitmap(), Picture_Captured.Width, Picture_Captured.Height, outputFile);

            string pathFileName = "\\SD\\picture_captured.bmp";
            try
            {
                File.WriteAllBytes(pathFileName, outputFile);
                Debug.Print("Saved picture_captured.bmp to file: " + pathFileName);
            }
            catch (Exception ex)
            {
                Debug.Print("Message: " + ex.Message + "  Inner Exception: " + ex.InnerException);
            }
        }

I found it out:

Hi,

that does not work. I think the reason is that I can’t get the captured picture.


new Bitmap(camera.CurrentPictureResolution.Width, camera.CurrentPictureResolution.Height)

Does somebody a propose?

I don’t get a syntax error but the image is always black.

I assume you are trying to open the file on a pc?
i’d take a step back. can you capture and display a picture successfully? if so, then write to file, and reopen on fez (not pc). if that works, its the file format you need to look at…

How many colors do you have per pixel?
If I remember right, then bmp’s with 256 colors are always saved using a palette (table with color value for each pixel value (0-255).
May be it’s just a bad palette.

camera.CurrentPictureResolution.Width returns the width of the picture but I want to get the picture. I used the method makeBitmap() of GT.picture (the captured picture by the camera) and so solved this issue.

Hi SB,

I tried following code below and I noticed that you do not need to do all this work around.

follows:


private void camera_PictureCaptured(Camera sender, GT.Picture picture)
{
	/////////////////////////
	//First try - Does OK
	Bitmap image = new Bitmap(320, 240);
	image.DrawRectangle(Colors.Blue, 1, 0, 0, 320, 240, 0, 0, Colors.Blue, 0, 0, Colors.Blue, 0, 0, Bitmap.OpacityOpaque);
	image.DrawLine(Colors.Orange, 1, 0, 0, 320 - 1, 0);
	image.DrawLine(Colors.Red, 1, 0, 0, 0, 240 - 1);
	image.DrawLine(Colors.Yellow, 1, 320 - 1, 240 - 1, 0, 240 - 1);
	image.DrawLine(Colors.Magenta, 1, 320 - 1, 240 - 1, 320 - 1, 0);
	image.DrawEllipse(Colors.Red, 320 / 2, 240 / 2, 50, 50);

	byte[] outputFile = new byte[image.Width * image.Height * 3 + 54];

	Util.BitmapToBMPFile(image.GetBitmap(), image.Width, image.Height, outputFile);

	File.WriteAllBytes("\\SD\\CreatedBMPFile.bmp", outputFile);

	/////////////////////////
	//Second try - Does OK too
	display_T35.SimpleGraphics.DisplayImage(picture, 5, 5);

	string fileName = "DSC_" + DateTime.Now.Ticks.ToString();
	string pathFileName = "\\SD\\" + fileName + ".bmp";

	Bitmap Picture_Captured = picture.MakeBitmap();            
	byte[] imageByte = picture.PictureData;
	//byte[] imageByte = new byte[Picture_Captured.Width * Picture_Captured.Height * 3 + 54];
	//Util.BitmapToBMPFile(Picture_Captured.GetBitmap(), Picture_Captured.Width, Picture_Captured.Height, imageByte);

	try
	{
		File.WriteAllBytes(pathFileName, picture.PictureData);
		Debug.Print("Saved file to: " + pathFileName);
	}
	catch (Exception ex)
	{
		Debug.Print("Message: " + ex.Message + "  Inner Exception: " + ex.InnerException);
	}
}

Now I’m trying to convert it to other image format like JPG eg.

Regards,
Andrw Paes

1 Like

Seems like a lot of work going on here and while I don’t frame my pictures I have this running right now on a project (uses the Raptor mainboard).


        private void camera_PictureCaptured(Camera sender, GT.Picture picture)
        {
            _picture = picture;

            //sets the background of the touch display using Glide
            window.BackImage = picture.MakeBitmap();
            window.Invalidate();

            if (sdCard.IsCardMounted)
            {
                DateTime now = DateTime.Now;

                string filename = DateTime.Now.ToString("yyyy-MMdd-HH-mm") + ".jpg";
                try
                {
                    sdCard.GetStorageDevice().WriteFile(filename, picture.PictureData);
                }
                catch (Exception ex)
                {

                    Debug.Print("Message: " + ex.Message + "  Inner Exception: " + ex.InnerException);
                }
            }
        }