Click here to Skip to main content
11,484,752 members (74,765 online)
Click here to Skip to main content

Camera_Net Library

, 14 Jan 2015 LGPL3 64.9K 16.1K 212
Rate this:
Please Sign up or sign in to vote.
Camera in .NET application using .NET library (DirectShow).

Complicated sample of Camera_Net library use. Stranger on television.

This article presents a library Camera_Net for working with cameras and video-inputs in .NET easily. Library uses DirectShow and includes samples of use: two simple samples and a complicated one.

Introduction

What is DirectShow?

First of all, in this article, we'll use DirectShow for rendering video streams from cameras. So what is DirectShow? DirectShow is a multimedia framework made by Microsoft guys. This framework was made very powerful and complicated, so it almost allows to make a home semi-pro video studio. But this huge complexity makes it hard to use. You need to make tons of code to simply render video from your web-camera to your application form.

Advantages and disadvantages of DirectShow

Advantages

  1. Powerful.
    It can do everything you need. If not, write your filters and use them in filter graph.
  2. Part of the Operating System.
    All Windows versions (XP, Vista, 7, 8) support it out of box. No need to install anything.
  3. Supported by all vendors.
    Almost every camera you can buy has drivers to work nicely with DirectShow.

Disadvantages

  1. It's not cross-platform.
    It's based on COM technology and is not cross-platform. It will work well only on Windows. It's also a proprietary framework as most of stuff Microsoft does.
    However, if you're using managed code (e.g. C#), your application is probably already not cross-platform.
  2. Native code.
    The framework doesn't support managed API.
    But you can use open source wrappers like DirectShowNet library to overcome this disadvantage.
  3. Too complicated and low-level to be used.
    To render your webcam, you should make a lot of coding work.
    But using wrappers like Camera_Net library from this article, you can do a lot of stuff with your camera easily.

So, it's up to you to use DirectShow or not as it has advantages and disadvantages. But if you do, maybe this article is for you.

DirectShow rendering graph

The main idea of audio/video rendering in DirectShow is to build a filter graph and run it. Filter Graph — is a graph with blocks (graph filters). Some of these blocks are video/audio sources (e.g. cameras, tuners), some are renders and others are filters that can change or transform multimedia streams. Also these graph filters have pins — points for connecting filters to each other. So, cameras have output pin or pins, renders have input pins and most of other filters have both types.

To render your camera, you can make a very simple filter graph:

The source filter is connected to simple Video Renderer. The source filter is available because operation system found it, sometimes with the help of vendor's camera drivers. The render is standard filter, you can consider it as a part of DirectShow framework. No additional filters were used.

Let's say we want to take pictures of video-stream and draw over video-output (video overlay). The graph will become more complicated:

Here Tee Filter is a standard filter to duplicate multimedia stream. One output stream of it goes to render and another goes to SampleGrabber to take pictures (snapshots). SampleGrabber is a standard filter for getting frames from video stream. In this graph, the Video Mixing Renderer is used instead of simple Video Renderer filter. Video Mixing Renderer is a standard filter that supports video overlay for rendered output.

If the source filter (camera, tuner) has different output multimedia format (media type), it can be necessary to add additional intermediate filters for conversion on the fly. It makes filter graph bigger and more sophisticated:

DirectShow can connect pins with distinct media types in an “intelligent” way by enumerating available intermediate filters installed in operation system and choosing one by itself, but the result sometimes can be not as good as you expected.

Filter graph images were made in an open source tool GraphStudioNext, which I recommend for everyone who is messing with filter graph. It's a great free software tool for building and debugging of filter graphs. It was made as a replacement of outdated buggy GraphEdit from Microsoft.

Managed Library as a Wrapper over DirectShow

To ignore all this mess with DirectShow graph, I would recommend to use a managed wrapper, which will hide all this low-level magic. It will allow you to overcome the third disadvantage of DirectShow — complexity and low-level coding.

In this article, I'll describe usage and features of Camera_Net library. This library was created because I've failed to find a library among dozens of open source solutions that would support what I wanted: easy use of camera as a managed component, ability to change the camera resolution, take frame snapshots, show overlay images over the frame. So this library was created!

Some of the features of this library:
  1. Select camera
  2. Select resolution
  3. Show camera's output
  4. Overlay image on the frame
  5. Take snapshots of the frame
  6. Change TV mode (PAL, NTSC, etc.)
  7. Display dialog windows of the camera (from drivers)
  8. Get a list of available cameras and resolutions

Classes CameraControl and Camera are the main classes in the library. Camera is a class for interaction with cameras. CameraControl is a UserControl-wrapper for Camera class, easy to use out of box.

Additionally, there are several public classes: Resolution, VideoInput, camera selection class, etc. The library uses DirectShowLib (license LGPL 2.1 or later), which is a very thin wrapper of DirectX COM-interfaces, so that the losses of productivity from the use of managed code instead of the native one are minimal.

All sources of library and examples are available on GitHub. You can post issues, pull requests or forks there.

DirectShow is Windows only, so the library is also available only for Windows.

Using Camera_Net

The most simple way to use this library is to add it in your application as Control. The class CameraControl (which is inherited from UserControl) is responsible for that. This approach is recommended.

The library includes samples of use in WinForms, in particular examples of a simple implementation of the component in your application, and more complicated example that shows almost all features of the library.

So, add the library project or a compiled assembly to references, add CameraControl to your application and add using directives:

using Camera_NET;
using DirectShowLib; 
Get list of available devices

Firstly, you probably want to get list of available cameras and video inputs in the system:

// Camera choice helper class
CameraChoice _CameraChoice = new CameraChoice(); 
_CameraChoice.UpdateDeviceList();

Now _CameraChoice.Devices includes all available devices (it's List<DsDevice>). The list will be empty if DirectShow didn't find any devices.

In samples, you can see the whole code with camera and resolution selection via ComboBox-es with human-readable names of cameras. To make this article short, let's simply use the first one (with zero index).

Moniker (device identification)

In terms of DirectShow, the camera device has a unique identification — a Moniker (for more information, please read about System.Runtime.InteropServices.ComTypes.IMoniker interface).

You can get moniker of device via Mon property of DsDevice:

var moniker = _CameraChoice.Devices[your_index].Mon; 
Get list of available resolutions

You can get a list of available resolutions (it camera's driver supports this function) using static function Camera.GetResolutionList(moniker):

ResolutionList resolutions = Camera.GetResolutionList(moniker); 
Start camera output

Finally, when you've chosen camera device and resolution you can set camera to CameraControl:

cameraControl.SetCamera(moniker, resolutions[your_index_of_resolution]);

or you can start with default resolution (usually it's a VGA mode 640x480 but not always):

cameraControl.SetCamera(moniker, null); 
Close camera

On camera changing or application exit, you can (not always necessary) close current camera:

cameraControl.CloseCamera();

It will free camera for other applications, dispose DirectShow graph and some other resources.

And a Lot More!

Library allows a lot more:

  • Take snapshots of video output SnapshotOutputImage() or snapshot of source frame SnapshotSourceImage().
  • Show overlay image (bitmap) over video, mixer supports transparency (properties MixerEnabled, OverlayBitmap, GDIAlphaValue, GDIColorKey and etc).
  • Use video input devices with crossbar and work with crossbar (CrossbarAvailable, VideoInput).
  • Display dialog windows of the camera (from drivers): DisplayPropertyPage_Device(), DisplayPropertyPage_Crossbar(), DisplayPropertyPage_CaptureFilter()
  • Work with TV mode (PAL, NTSC, etc.): SetTVMode(), GetTVMode().
  • Change camera and resolution (use SetCamera() again).

For more information, you can look at samples included with library.

Simple sample of Camera_Net library use.

License

Camera_NET library license

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

Samples license

While the Camera_Net library is covered by LGPL, the samples are released as PUBLIC DOMAIN. So, you can use code from these samples in your free or proprietary project without any limitations.

Credits

The author of this article and the Camera_Net library is free5lot. All sources of library and examples are available on github.

This project uses a library DirectShowLib (DirectShow.Net library) which is covered by LGPL 2.1 or later.

History

Version 1.1.2
  • Attributes [Browsable(false)] and [DesignerSerializationVisibility] were added to properties that couldn't be used in a form editor and throw warnings during serialization.

  • PostBuildEvent with release DLL output
Version 1.1.1
  • Fixed an problem when moving from one monitor to second one
  • .NET2 and .NET3.5 assemblies are available in addition to .NET4.
Version 1.1.0

Some major fixes (see github log). Some cleaning of code. Added new sample — "VerySimple".

Version 1.0.0

A first public open source release of Camera_Net library.

Links to Related Open Source Projects

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

free5lot
Software Developer
Russian Federation Russian Federation
codeproject@free5lot.com

Comments and Discussions

 
QuestionProgressively Slower Refresh Rate Pin
Member 930645920-May-15 8:39
memberMember 930645920-May-15 8:39 
QuestionCamera Resolutions between video and camera Pin
Gary.Bell16-Apr-15 16:34
memberGary.Bell16-Apr-15 16:34 
AnswerRe: Camera Resolutions between video and camera Pin
free5lot21-Apr-15 2:09
memberfree5lot21-Apr-15 2:09 
Questiondraw on each frame Pin
Member 988367212-Apr-15 21:01
memberMember 988367212-Apr-15 21:01 
AnswerRe: draw on each frame Pin
free5lot21-Apr-15 1:51
memberfree5lot21-Apr-15 1:51 
QuestionCant make it work with x86 compilation Pin
Ami Drutman4-Apr-15 6:15
memberAmi Drutman4-Apr-15 6:15 
AnswerRe: Cant make it work with x86 compilation Pin
free5lot21-Apr-15 1:48
memberfree5lot21-Apr-15 1:48 
QuestionIntegrate with barcode scanning Pin
JK Tan25-Mar-15 23:56
memberJK Tan25-Mar-15 23:56 
AnswerRe: Integrate with barcode scanning Pin
free5lot26-Mar-15 0:33
memberfree5lot26-Mar-15 0:33 
QuestionPass captured frame to varaible for further processing Pin
Member 1126099810-Feb-15 21:54
memberMember 1126099810-Feb-15 21:54 
AnswerRe: Pass captured frame to varaible for further processing Pin
free5lot11-Feb-15 0:17
memberfree5lot11-Feb-15 0:17 
QuestionWebcam resolution change Pin
Piero Viano27-Jan-15 21:53
memberPiero Viano27-Jan-15 21:53 
AnswerRe: Webcam resolution change Pin
free5lot29-Jan-15 1:31
memberfree5lot29-Jan-15 1:31 
QuestionVb 2010 Pin
Pablo Fabian23-Jan-15 12:17
memberPablo Fabian23-Jan-15 12:17 
QuestionAnyone knows how to capture video stream? Pin
mikeduglas22-Jan-15 6:45
membermikeduglas22-Jan-15 6:45 
QuestionSupport for WPF?? Pin
tlhintoq16-Jan-15 4:32
membertlhintoq16-Jan-15 4:32 
AnswerRe: Support for WPF?? Pin
tlhintoq16-Jan-15 5:43
membertlhintoq16-Jan-15 5:43 
NewsSmall fix uploaded! (about Error with post-build copy to Release folder) [modified] Pin
free5lot15-Jan-15 20:25
memberfree5lot15-Jan-15 20:25 
QuestionMy Vote of 5 Pin
RogueClay15-Jan-15 9:48
professionalRogueClay15-Jan-15 9:48 
AnswerRe: My Vote of 5 [modified] Pin
free5lot15-Jan-15 20:26
memberfree5lot15-Jan-15 20:26 
QuestionHelpFul Document Pin
hessam9415-Jan-15 7:42
memberhessam9415-Jan-15 7:42 
AnswerRe: HelpFul Document Pin
free5lot15-Jan-15 20:33
memberfree5lot15-Jan-15 20:33 
GeneralMy vote of 5 Pin
Member 857799214-Jan-15 13:48
memberMember 857799214-Jan-15 13:48 
GeneralRe: My vote of 5 Pin
free5lot14-Jan-15 22:15
memberfree5lot14-Jan-15 22:15 
QuestionGreat Application Pin
Member 1101174313-Jan-15 18:57
memberMember 1101174313-Jan-15 18:57 
GeneralRe: Great Application [modified] Pin
free5lot13-Jan-15 20:38
memberfree5lot13-Jan-15 20:38 
QuestionThanks for the good job but I have a problem implementing it. Pin
Member 112772379-Jan-15 10:00
memberMember 112772379-Jan-15 10:00 
AnswerRe: Thanks for the good job but I have a problem implementing it. Pin
free5lot9-Jan-15 21:17
memberfree5lot9-Jan-15 21:17 
GeneralRe: Thanks for the good job but I have a problem implementing it. Pin
Member 112772379-Jan-15 21:45
memberMember 112772379-Jan-15 21:45 
GeneralRe: Thanks for the good job but I have a problem implementing it. Pin
free5lot9-Jan-15 22:27
memberfree5lot9-Jan-15 22:27 
GeneralRe: Thanks for the good job but I have a problem implementing it. Pin
Member 1127723710-Jan-15 0:30
memberMember 1127723710-Jan-15 0:30 
GeneralRe: Thanks for the good job but I have a problem implementing it. Pin
free5lot10-Jan-15 0:34
memberfree5lot10-Jan-15 0:34 
GeneralRe: Thanks for the good job but I have a problem implementing it. Pin
Member 1127723710-Jan-15 0:47
memberMember 1127723710-Jan-15 0:47 
GeneralRe: Thanks for the good job but I have a problem implementing it. Pin
free5lot10-Jan-15 0:56
memberfree5lot10-Jan-15 0:56 
GeneralRe: Thanks for the good job but I have a problem implementing it. Pin
Member 1127723710-Jan-15 1:07
memberMember 1127723710-Jan-15 1:07 
GeneralRe: Thanks for the good job but I have a problem implementing it. Pin
free5lot10-Jan-15 1:11
memberfree5lot10-Jan-15 1:11 
GeneralRe: Thanks for the good job but I have a problem implementing it. Pin
Member 1127723710-Jan-15 1:16
memberMember 1127723710-Jan-15 1:16 
GeneralRe: Thanks for the good job but I have a problem implementing it. Pin
Member 1127723710-Jan-15 1:19
memberMember 1127723710-Jan-15 1:19 
GeneralRe: Thanks for the good job but I have a problem implementing it. Pin
free5lot10-Jan-15 1:26
memberfree5lot10-Jan-15 1:26 
GeneralRe: Thanks for the good job but I have a problem implementing it. Pin
Member 1127723710-Jan-15 1:42
memberMember 1127723710-Jan-15 1:42 
GeneralRe: Thanks for the good job but I have a problem implementing it. Pin
free5lot10-Jan-15 2:13
memberfree5lot10-Jan-15 2:13 
GeneralRe: Thanks for the good job but I have a problem implementing it. Pin
Member 1127723710-Jan-15 11:08
memberMember 1127723710-Jan-15 11:08 
GeneralRe: Thanks for the good job but I have a problem implementing it. Pin
free5lot10-Jan-15 19:37
memberfree5lot10-Jan-15 19:37 
GeneralRe: Thanks for the good job but I have a problem implementing it. Pin
RogueClay15-Jan-15 9:45
professionalRogueClay15-Jan-15 9:45 
GeneralRe: Thanks for the good job but I have a problem implementing it. [modified] Pin
RogueClay15-Jan-15 10:04
professionalRogueClay15-Jan-15 10:04 
AnswerRe: Thanks for the good job but I have a problem implementing it. Pin
free5lot15-Jan-15 20:23
memberfree5lot15-Jan-15 20:23 
Questiondirect show. Pin
jfriedman13-Dec-14 4:07
professionaljfriedman13-Dec-14 4:07 
AnswerRe: direct show. [modified] Pin
free5lot13-Dec-14 9:13
memberfree5lot13-Dec-14 9:13 
GeneralRe: direct show. Pin
jfriedman13-Dec-14 10:45
professionaljfriedman13-Dec-14 10:45 
QuestionChange FPS configuration Pin
Gerson Silva Filho4-Dec-14 4:07
memberGerson Silva Filho4-Dec-14 4:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 14 Jan 2015
Article Copyright 2013 by free5lot
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid