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
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:
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:
Title=”MainWindow” Height=”150″ Width=”150″>
<Label Name=”_textBlock” />
Add the following code to your MainWindow.xaml.cs:
SensorHelper<AmbientLightSensor, LuminousIntensity> _lightSensor;
_lightSensor = new SensorHelper<AmbientLightSensor,
MessageBox.Show(“Error while loading sensor. \n” +
“Make sure you run the Virtual Light Sensor “ +
“before the application”, “Error”);
void _lightSensor_PropertyChanged(object sender,
if (_lightSensor.Value.Intensity > 200)
_textBlock.Content = “Light”;
_textBlock.Foreground = Brushes.Black;
_textBlock.Background = Brushes.White;
_textBlock.Content = “Dark”;
_textBlock.Foreground = Brushes.White;
_textBlock.Background = Brushes.Black;
The end result:
You can find the source code for this demo application here.
That’s it for now,