このコードは私にとってはうまくいきます。ほとんどのコードは Afroge Samples から取得しました。
private VideoCaptureDevice videoDevice;
private FilterInfoCollection videoDevices;
private VideoCapabilities[] videoCapabilities;
public Bool SetCamera(Cameras camera, int camDevice, CameraResolution camResolution,
int exposureValue, int zoomValue, int focusValue)
{
// Enumerate video devices
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
//Select camera according to specified index
videoDevice = new VideoCaptureDevice(videoDevices[camDevice].MonikerString);
//Get video capabilities for setting the resoluton
videoCapabilities = videoDevice.VideoCapabilities;
if (camera == Cameras.C910)
{
switch (camResolution)
{
case CameraResolution.A_640x480:
videoDevice.VideoResolution = videoCapabilities[0];
break;
case CameraResolution.B_800x600:
videoDevice.VideoResolution = videoCapabilities[14];
break;
case CameraResolution.C_960x720:
videoDevice.VideoResolution = videoCapabilities[16];
break;
case CameraResolution.D_1280x720:
videoDevice.VideoResolution = videoCapabilities[17];
break;
case CameraResolution.E_1920x1080:
videoDevice.VideoResolution = videoCapabilities[24];
break;
default:
videoDevice.VideoResolution = videoCapabilities[0];
break;
}
}
else if (camera == Cameras.C920)
{
switch (camResolution)
{
case CameraResolution.A_640x480:
videoDevice.VideoResolution = videoCapabilities[0];
break;
case CameraResolution.B_800x600:
videoDevice.VideoResolution = videoCapabilities[10];
break;
case CameraResolution.C_960x720:
videoDevice.VideoResolution = videoCapabilities[12];
break;
case CameraResolution.D_1280x720:
videoDevice.VideoResolution = videoCapabilities[14];
break;
case CameraResolution.E_1920x1080:
videoDevice.VideoResolution = videoCapabilities[16];
break;
default:
videoDevice.VideoResolution = videoCapabilities[0];
break;
}
}
else
{
videoDevice.VideoResolution = videoCapabilities[0];
}
try
{
videoDevice.SetCameraProperty(
CameraControlProperty.Zoom,
zoomValue,
CameraControlFlags.Manual);
videoDevice.SetCameraProperty(
CameraControlProperty.Focus,
focusValue,
CameraControlFlags.Manual);
videoDevice.SetCameraProperty(
CameraControlProperty.Exposure,
exposureValue,
CameraControlFlags.Manual);
}
catch (Exception ex)
{
MessageBox.show(ex.ToString());
}