0

I have code which gets the photo from a web camera. I need to write a mechanism that would be expected to complete this process. How do I do this? I have the following code:

ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();
var memStream3 = new Windows.Storage.Streams.InMemoryRandomAccessStream();
var mediaCaptureMgr1 = new MediaCapture();
await mediaCaptureMgr1.InitializeAsync();
mediaCaptureMgr1.SetPreviewMirroring(true);
await mediaCaptureMgr1.CapturePhotoToStreamAsync(imageProperties, memStream3);
await memStream3.FlushAsync();
memStream3.Seek(0);
WriteableBitmap wb1 = new WriteableBitmap(320, 240);
wb1.SetSource(memStream3);
//1
while (true)
{
    await Task.Delay(TimeSpan.FromSeconds(0.1));
    //if CapturePhotoToStreamAsync finished? OR memStream3 not null?
    //break;
}
//2

If I start to do anything with wb1 at //1, I will not work because wb1 = null. If I start doing it at //2 the wb1! = null because I waited until all the async function are completed.

4
  • Do you want to replace the while-loop? Commented Apr 4, 2013 at 8:28
  • 2
    I don't see why you have the while loop in there at all. I also don't understand what you are asking. Please be more specific here... Commented Apr 4, 2013 at 8:30
  • 2
    When you await the CapturePhotoToStreamAsync method, the method already finished in the followed code line. That's the point of using await. Commented Apr 4, 2013 at 8:33
  • I specified in the first post Commented Apr 4, 2013 at 9:04

1 Answer 1

2

Instead of wb1.SetSource(memStream3); you should call await wb1.SetSourceAsync(memStream3); and all should be good. Otherwise you could set var wb1 = new WriteableBitmap(1, 1); and wait until wb1.PixelWidth grows bigger than 1 when SetSource is done, but this method with Task.Delay polling is suboptimal.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.