pythonimus pythonimus - 3 months ago 20
C# Question

How to write to InMemoryRandomAccessStream and then read?

I want to write an Image to a stream and read it afterwards.

Im on Win 10 UWP.

My Code:

InMemoryRandomAccessStream imrasIn = new InMemoryRandomAccessStream();
await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), imrasIn);
DetectedFaces = await _faceClient.DetectAsync(imrasIn.GetInputStreamAt(0).AsStreamForRead());


It does not work, DetectAsync gets an empty stream (Error: Image size is too small).

Do I need other classes?
CapturePhotoToStreamAsync
wants an
IRandomAccessStream
and
DetectAsync
wants a
Stream
.

Answer

I had to rewind the stream before reading (and after writing to it):

imrasIn.Seek(0);