Friday, July 6, 2012

CE: Sample Camera Dialog App

Here's a sample code for running the camera on a Windows CE/Mobile device

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace CamTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void LaunchCam_Click(object sender, EventArgs e)
        {
            string filename;
            CameraCaptureDialog cameraCapture = new CameraCaptureDialog();

            try
            {
                cameraCapture.Owner = null;
                string strAppDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
                cameraCapture.InitialDirectory = strAppDir;
                string fileName = "picture1";
                cameraCapture.DefaultFileName = fileName + ".jpg";
                cameraCapture.Title = "CamTest";
                cameraCapture.Resolution = new System.Drawing.Size(400, 640);
                cameraCapture.Mode = CameraCaptureMode.Still;
                cameraCapture.StillQuality = CameraCaptureStillQuality.Normal;
                cameraCapture.VideoTimeLimit = new TimeSpan(0, 0, 0);


                if (DialogResult.OK == cameraCapture.ShowDialog())
                {
                    System.Diagnostics.Debug.WriteLine(cameraCapture.FileName);
                    if (!File.Exists(cameraCapture.FileName))
                    {
                        throw new Exception("Error saving the photo");
                    }
                    filename = cameraCapture.FileName;
                    return;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {

                if (cameraCapture != null)
                {
                    cameraCapture.Dispose();
                    cameraCapture = null;
                }
            }
            return;

        }
    }
}

No comments: