How to Set Brightness and Exposure for a Webcam Using C# and MediaFoundation

It’s incredibly hard to find the easiest way to control a webcam using MediaFoundation. It’s not that tough to stream the video or snap a photo but changing contrast and brightness and exposure… phew.

A small useful video camera

So, I thought I’d embed some code to make it easy for folks. Once you have an IMFMediaSource object (essentially the video player) you can cast it to the two necessary control interfaces. These then have get/set/getrange methods to set or get the values.

Use the IAMCameraControl and IAMVideoProcAmp interfaces. Both of these are standard DirectShow interfaces that can be inherited from the IMFMediaSource in a standard COM fashion.

The trick is knowing the syntax and having the GUIDS and the definitions of the properties. Most of the work is documented here and there. The best source of information is Aforge.NET. If you go down the rabbithole of StackOverflow you'll be there for a long time and get nowhere.

The IAMCameraControl interface lets you control exposure and webcam stuff, for some reason. The IAMVideoProcAmp interface lets you control everything else, such as brightness, contrast, and gain. Note that in the code set below there’s also a small sample chunk showing how to use the interfaces once you have an IMFMediaSource.

Here’s the code:

A single golf clap? Or a long standing ovation?

By clapping more or less, you can signal to us which stories really stand out.