DCSIMG
Using Windows 7 Light Sensor in Your Application - Arik Poznanski's Blog

Arik Poznanski's Blog

It CAN be done with .NET

News

MVP

MCC

CodeProject MVP

MCPD

MCTS

Subscribe to my blog by email

Arik Poznanski LinkedIn Profile

Email: arik.com at gmail dot com
or, use this form

Locations of visitors to this page


Sela Group

Sela Canada

DZone MVB

Links

Official Blogs

WPF / SL Blogs

Developers Blogs

Using Windows 7 Light Sensor in Your Application

One of the new features we got with Windows 7 is a the Windows Sensor and Location Platform. This platform enables us, developers, to access a variety of sensors with a standard API. No more rewrite-entire-device-layer-because-we-changed-vendors issues. All that is required is that the hardware vendor will provide a Windows 7 compliant driver. Microsoft practically opened this market to competition, so we can expect allot of these sensors in the coming future.

Ambient Light Sensor

One sensor type which becomes increasingly poplar with laptops is the Ambient Light Sensor. This sensor type measures the amount of light in the computer surroundings.

There are numerous applications for such a sensor, just to name a few:

  • Change application theme to a darker one in case there's almost no surrounding light, to avoid stress on the user eyes.
  • Use high contrast colors in extreme lightning condition such as direct sun or no light.
  • Reduce screen brightness according to current lightning conditions.
  • Reduce sound volume in applications when the light is low (maybe someone is sleeping).

Using Ambient Light Sensor in a .NET application

Using the light sensor is pretty simple in .NET with the help of Windows API Code Pack and another helper file from the Windows 7 Training Kit for Developers.

I’ll show you an example using a WPF application but the same will work with a WinForms application.

Installing Virtual Light Sensor

First step is to install the Windows 7 SDK. Among other things it contains a virtual light sensor you can use in case you don’t actually have a light sensor on your computer.

For installing the virtual light sensor follow these instructions.

In case you fail to install virtual light sensor, make sure you install the one which is suitable for your operating system. 64bit systems need to install the 64bit version of the virtual light sensor driver (the 64bit driver is stored in the Windows 7 SDK subfolder named x64).

After you have successfully installed the virtual light sensor driver you can control the simulated light sensor using the utility VirtualLightSensor.exe:

image

Adding Ambient Light Sensor to Our Application

Now we will open our WPF application, named LightSensorDemo.

Add a reference to Microsoft.WindowsAPICodePack.Sensors.dll

Add the file SensorHelper.cs to the project. This file appears in one of the examples of the Windows 7 Training Kit for Developers. It provides a convenient facade around the Windows API Code Pack Sensor API.

Note, this demo is not a great example for WPF best practices..

Add a label to your MainWindow.xaml:

<Window x:Class="LightSensorDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="150" Width="150">
    <Grid>
        <Label Name="_textBlock" />
    </Grid>
</Window>

Add the following code to your MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    SensorHelper<AmbientLightSensor, LuminousIntensity> _lightSensor;

    public MainWindow()
    {
        InitializeComponent();

        try
        {
            _lightSensor = new SensorHelper<AmbientLightSensor,
                                            LuminousIntensity>();
            _lightSensor.Initialize();
            _lightSensor.PropertyChanged +=
                new PropertyChangedEventHandler(
                    _lightSensor_PropertyChanged);
        }
        catch (SensorPlatformException)
        {
            MessageBox.Show("Error while loading sensor. \n" +
                      "Make sure you run the Virtual Light Sensor " +
                      "before the application", "Error");
        }
    }

    void _lightSensor_PropertyChanged(object sender,
                                      PropertyChangedEventArgs e)
    {
        Dispatcher.BeginInvoke(new Action(
            delegate()
            {
                if (_lightSensor.Value.Intensity > 200)
                {
                    _textBlock.Content = "Light";
                    _textBlock.Foreground = Brushes.Black;
                    _textBlock.Background = Brushes.White;
                }
                else
                {
                    _textBlock.Content = "Dark";
                    _textBlock.Foreground = Brushes.White;
                    _textBlock.Background = Brushes.Black;
                }
            }));
    }
}

The end result:

image

image

You can find the source code for this demo application here.

That’s it for now,
Arik Poznanski.

kick it on DotNetKicks.com Shout it

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# June 29, 2010 1:16 PM

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# June 29, 2010 1:17 PM

Using Windows 7 Light Sensor in Your Application – Arik … : News IT said:

Pingback from  Using Windows 7 Light Sensor in Your Application &#8211; Arik &#8230; : News IT

# June 29, 2010 4:10 PM

noneed.info » Blog Archive » Using Windows 7 Light Sensor in Your Application – Arik … said:

Pingback from  noneed.info  &raquo; Blog Archive   &raquo; Using Windows 7 Light Sensor in Your Application &#8211; Arik &#8230;

# June 29, 2010 4:24 PM

Daily MultiNews » Using Windows 7 Light Sensor in Your Application – Arik … said:

Pingback from  Daily MultiNews &raquo; Using Windows 7 Light Sensor in Your Application &#8211; Arik &#8230;

# June 29, 2010 4:41 PM

Using Windows 7 Light Sensor in Your Application – Arik … | Webstuffz.org said:

Pingback from  Using Windows 7 Light Sensor in Your Application &#8211; Arik &#8230; | Webstuffz.org

# June 29, 2010 5:11 PM

Using Windows 7 Light Sensor in Your Application – Arik … said:

Pingback from  Using Windows 7 Light Sensor in Your Application &#8211; Arik &#8230;

# June 29, 2010 5:14 PM

Twitter Trackbacks for Using Windows 7 Light Sensor in Your Application - Arik Poznanski's Blog [microsoft.co.il] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Using Windows 7 Light Sensor in Your Application - Arik Poznanski's Blog         [microsoft.co.il]        on Topsy.com

# June 30, 2010 2:57 AM

pedror414 said:

I was not familiar with all this written in this blog. I came to learn a lot from this. Thanks

# June 25, 2011 9:52 AM

Arik Poznanski's Blog said:

I just had a cool idea! (actually I had it like 6 month ago, but bear with me). My idea involves mixing

# August 13, 2011 1:34 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: