# UsbCamera **Repository Path**: sun_guang_ming/UsbCamera ## Basic Information - **Project Name**: UsbCamera - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2022-03-11 - **Last Updated**: 2022-03-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # UsbCamera C# source code for using USB camera in WinForms/WPF. With only single CSharp source code. No external library required. # How to use Add UsbCamera.cs to your project. ```C# // [How to use] // check USB camera is available. string[] devices = UsbCamera.FindDevices(); if (devices.Length == 0) return; // no camera. // check format. int cameraIndex = 0; UsbCamera.VideoFormat[] formats = UsbCamera.GetVideoFormat(cameraIndex); for(int i=0; i pbxScreen.Image = camera.GetBitmap(); timer.Start(); // release resource when close. this.FormClosing += (s, ev) => timer.Stop(); this.FormClosing += (s, ev) => camera.Stop(); ``` # if WPF By default, GetBitmap() returns image of System.Drawing.Bitmap. If WPF, define 'USBCAMERA_WPF' symbol that makes GetBitmap() returns image of BitmapSource. # Adjust Tilt, Zoom, Exposure, Brightness, Contrast, etc... ```C# // adjust properties. UsbCamera.PropertyItems.Property prop; prop = camera.Properties[DirectShow.CameraControlProperty.Exposure]; if (prop.Available) { // get current value var val = prop.GetValue(); // set new value var min = prop.Min; var max = prop.Max; var def = prop.Default; var step = prop.Step; prop.SetValue(DirectShow.CameraControlFlags.Manual, def); } prop = camera.Properties[DirectShow.VideoProcAmpProperty.WhiteBalance]; if (prop.Available && prop.CanAuto) { prop.SetValue(DirectShow.CameraControlFlags.Auto, 0); } ``` # Special Thanks. This project make use of a part of source code of the project below. Thank you! * DShowLib (author: Mr. M.Oshikiri) - http://www.geocities.co.jp/SiliconValley/7406/tips/dshowdll/dshowdll0.html * DSLab (author: cogorou) - https://github.com/cogorou/DSLab # Example. You can make H264 recorder by using UsbCamera and OpenH264Lib.NET and [AviWriter](https://github.com/secile/MotionJPEGWriter/blob/master/source/AviWriter.cs)(in MotionJPEGWriter). See README.md in [OpenH264Lib.NET](https://github.com/secile/OpenH264Lib.NET). You can make MotionJPEG movie with audio, see README.md in [AudioIO](https://github.com/secile/AudioIO).