I downloaded this basic camera sample code from the Windows Phone Dev Center. Then, in that project, I added these lines at the top of the void cam_CaptureImageAvailable(Object sender, Microsoft.Devices.ContentReadyEventArgs e) method. Here the variable cam is of type PhotoCamera.
Size camRes = cam.PreviewResolution;
int[] imgdata = new int[(int)((camRes.Height*camRes.Width)+1)];
cam.GetPreviewBufferArgb32(imgdata);
//Code to perform manipulations on the frame follows
I’m getting an InvalidOperationException on cam.GetPreviewBufferArgb32(imgdata);. The relevant troubleshooting exceptions page on msdn says that
An InvalidOperationException exception is thrown in cases when the failure to invoke a method is caused by a reason other than an invalid argument. This may be thrown by .NET Framework methods when the underlying Win32 method cannot be invoked.
Also mentioned on that troubleshooting page is that graphic objects cannot be accessed from outside threads that they are declared in. But, this is a basic app and runs on one thread.
I’m a complete beginner at developing apps on Windows Phone and cannot figure out the cause of this exception. Please give suggestions for the cause of the exception.
Can be solved by spawning another thread that fetches the preview frame as in this sample.