private static Size GetInitialResolution(CameraSensorLocation location, int videoWidth, int videoHeight) { var resolutions = AudioVideoCaptureDevice.GetAvailablePreviewResolutions(location); // find the closest resolution if (videoWidth > 0 || videoHeight > 0) { var closest = new List <Size>(); var distance = -1; foreach (var res in resolutions) { if (distance < 0) { closest.Add(res); distance = GetSizeDistance(res, videoWidth, videoHeight); } else { var d = GetSizeDistance(res, videoWidth, videoHeight); if (d == distance) { closest.Add(res); } else if (d < distance) { closest = new List <Size>(); closest.Add(res); distance = d; } } } resolutions = closest; } if (resolutions.Count == 0) { var error = "The camera does not support any resolutions."; Log.Error(error); throw new Exception(error); } return(resolutions[0]); }