preload
Mar 14

In this post I will write about what I have learned while working with camera on a Windows Mobile (.NET Compact Framework) project.

The first thing you need to do is to check if the device running your application supports camera. This is easily done by using the SystemState.CameraPresent property. To use this property you must add a reference to Microsoft.WindowsMobile.Status.dll and to Microsoft.WindowsMobile.dll. The following code is then all you need to check if there is a camera present:

        public bool CameraPresent()
        {
            return SystemState.CameraPresent;
        }

The next step is to open the camera viewfinder and take a snapshot. There are several ways to do this, one option is to use the built in CameraCaptureDialog. This is probably the easiest way to do it but you are then using the default Windows Mobile camera capture dialog and you can not graphically embed it in your application. To use CameraCaptureDialog you must reference Microsoft.WindowsMobile.Forms.dll in your project. The following code shows how to use the CameraCaptureDialog.

        private void CameraCapture(bool video)
        {
            CameraCaptureDialog cameraCapture = new CameraCaptureDialog();

            //Control that owns the CameraCaptureDialog.
            cameraCapture.Owner = this;
            //Directory to save the captured media.
            cameraCapture.InitialDirectory = @"\My Documents";
            //Header title.
            cameraCapture.Title = "Camera Demo";
            //Video clip quality.
            cameraCapture.VideoTypes = CameraCaptureVideoTypes.Messaging;
            //Camera resolution.
            cameraCapture.Resolution = new Size(176, 144);
            // Limited to 15 seconds of video.
            cameraCapture.VideoTimeLimit = new TimeSpan(0, 0, 15);
            //Set capture mode to Video with audio if parameter video is true,
            //else set capture mode to still image.
            if (video)
            {
                cameraCapture.Mode = CameraCaptureMode.VideoWithAudio;
                cameraCapture.DefaultFileName = @"videotest.3gp";
            }
            else
            {
                cameraCapture.Mode = CameraCaptureMode.Still;
                cameraCapture.DefaultFileName = @"imagetest.jpg";
            }

            if (DialogResult.OK == cameraCapture.ShowDialog())
            {
                //CameraCaptureDialog is completed and will return to
                //your application.
                //If you want to do something special when returning
                //from CameraCaptureDialog you can do it here.
            }
        }

Screenshot showing the CameraCaptureDialog. In the CameraCaptureDialog you also have a menu where the user can adjust camera settings

When the media is captured a preview screen is presented with a menu where the user can select what to do with the captured media. Selecting OK takes the user back to your application.

You can see that the image is saved in the "My Documents" directory with the file name "imagetest.jpg"

So if you want to capture images or videos from camera in a simple way and it is OK that the user “jumps” out of your application, CameraCaptureDialog is the way to do it.

In many applications this is not good enough and the camera capture view needs to be embedded in the application, meaning that you want the camera viewfinder to be a part of your form/control. Achieving this is a bit more complicated than using the CameraCaptureDialog. You can write your own C++ implementation of the camera which will give you full control over the camera. I did not want to spend a lot of time implementing this so I went for a third-party solution called DirectShowNETCF.

DirectShowNETCF is created by Alex Mogurenko and it is a library with camera controls that you can embed in your application. From the DirectShowNETCF page you can download a demo and try it but to get rid of the demo watermark (or get the source code) you need to buy it. Considering the time you save by not having to code this yourself I think the DirectShowNETCF is well worth its cost.

When working with media on a device with limited capacity you also need to consider and handle memory usage. I have experienced some OutOfMemoryExceptions when capturing images with large resolutions. Also consider media quality and size if you are to send the captured media from the device to a server.

Please let me know if you have used other solutions to work with camera on Windows Mobile applications.

Follow me on twitter @PerOla

Share & enjoy
You can subscribe to my comments feed to keep track of new comments.

7 Comments to “Working with camera on Windows Mobile applications (.NET Compact Framework)”

  1. shah says:

    hi there. Very easy to follow article you got here. I just got one question. I’m quite new to programming, and now I wanna try mobile apps. Just wanna ask, where do I need to put the code. I try putting in Form1_Load() event but when I run it in my mobile device, it just display a plain white screen.

  2. Hi shah,

    It should be sufficient to place the code within the Form1_Load() event. If you are running the app in emulator camera might not work. Try to run your code on a real device and see what happens. You can also put a breakpoint in the load method just to make sure that it is called.

  3. Paradox says:

    Hello,

    I have an app that needs to open the camera without showing the controls below (Just the camera view). And then shut it down from a parent form.

    Is there a way simple of doing this?

  4. Hi Paradox,

    As far as I know you can not remove the controls when using the CameraCaptureDialog.
    If you need to just display the camera view you should have a look at the DirectShowNETCF I mentioned in the article.

  5. Budi says:

    Hi, I’m newbie on windows mobile programming. Can you show full code for this example?

    Thanks

  6. Budi,

    Unfortunately I don’t have the source code for this anymore.

  7. Dphf Blog says:

    How difficult is it to get to learn creating apps like that. Is it possible to learn it by yourself?

No Pingbacks to “Working with camera on Windows Mobile applications (.NET Compact Framework)”

Leave a Reply

Subscribe to my comments feed

Subscribe to my feeds Follow me on Twitter
DZone MVB