Reputation: 662
I'm working on a camera app in Windows Phone 8.1 where I've implemented zoom using the standard screen pinch/expand gesture. The preview of the camera is linked to a CaptureElement
which has an attached ManipulationDelta
event handler. The following code works fine:
private MediaCapture CameraManager;
//initialize CameraManager
private void BaseCanvas_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
float zoomFactor = (float)(Math.Round(e.Delta.Expansion / (CameraManager.VideoDeviceController.ZoomControl.Step * 300), 0) * CameraManager.VideoDeviceController.ZoomControl.Step);
Debug.WriteLine("Zoom factor: " + zoomFactor);
ZoomFunction(zoomFactor);
}
private void ZoomFunction(float zoomFactor)
{
if (CameraManager.VideoDeviceController.ZoomControl.Value + zoomFactor < CameraManager.VideoDeviceController.ZoomControl.Max && CameraManager.VideoDeviceController.ZoomControl.Value + zoomFactor > CameraManager.VideoDeviceController.ZoomControl.Min)
{
CameraManager.VideoDeviceController.ZoomControl.Value = CameraManager.VideoDeviceController.ZoomControl.Value + zoomFactor;
}
}
The problem is, I want to be able to call the ZoomFunction
from outside of the event handler and zoom in/out by a fixed amount as well. So I simply tried this:
ZoomFunction(0.1F);
The above call, however, throws a System.Argument
exception with additional information "parameter is incorrect" when it reaches the line of code inside the if
block of the ZoomFunction
. I've tried different variants like ZoomFunction((float)(0.1))
and so on but nothing seems to work.
Clearly something's wrong with the argument since that's the only difference between the two calls, but I can't seem to figure out what. It isn't the magnitude of the argument since I've placed a check on it to ensure it doesn't exceed the camera's zoom value limits. Also, I've tried using values that are typically generated by the event handler as well but they too seem to make no difference.
SOLUTION:
It turns out that the zoom value has to be an integer multiple of the minimum step which can be found by accessing CameraManager.VideoDeviceController.ZoomControl.Step
. I simply rounded off 0.1 to the nearest multiple of the step size and the function works fine now.
Upvotes: 1
Views: 221
Reputation: 3589
The problem you were having is a result of a restriction set on the zoom factor. Your zoom factor was set to 0.1
, which is not an integral multiple of the your ZoomControl.Step
. This is why you get the parameter is incorrect
error (granted the error message could be a bit more informative). More information about this at this link. The solution would be, as you've figured out, to use an integer for the zoom value, specifically a multiple of your step frequency (which by the way makes sense, if your step frequency is 1
, you should only be able to have multiples of 1
as valid zoom factors (i.e. 1, 2, 3, etc.) as these are the only valid steps according to your step frequency.)
Just to add some more info in case the link goes dead, the article states that "if StepFrequency is not equal to ZoomControl.Step" you get the very same exception you are encountering. (In the article, StepFrequency is a value by which the zoom is incremented according to a slider - this is analogous to the manual zoom factor your are trying to apply). Perhaps it would be more accurate if the article stated "if StepFrequency is not a multiple of ZoomControl.Step", but at least it led us to the solution.
Upvotes: 3