PixelBuffer.AsStream() method obsolete?

1.2k Views Asked by At

I was checking the MS library.

and it looks like PixelBuffer.AsStream() is obsolete for the Universal App development.

I'm using Win10(build 10240) with VS2015 Community version.

Do I miss anything ?

Additionally, what's the best way to encode or write a stream into the WriteableBitmap from the bytes or integers array?

I was trying the method from the BitmapEncoder, but I could create it only this way:

BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(fileStream, decoder);
enc.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, W, H, 200, 200, sourcePixels);

and afterwards there was no modifications visible on the WriteableBitmap. otherwise the framework was throwing exceptions(outside of my code)

Decoder was working fine and I've get all pixels properly decoded and streamed to byte[] sourcePixels array.

2

There are 2 best solutions below

0
On

AsStream is not obsolete. It is valid as shown in the linked MSDN sample code. It is an extension method to make IBuffer management easier not directly a mother on IBuffer. See https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.windowsruntime.windowsruntimebufferextensions.asstream(v=vs.110).aspx

To use it put the following using at the top of the source file:

using System.Runtime.InteropServices.WindowsRuntime

Then you'll be able to call PixelBuffer.AsStream as shown in the documentation's sample:

// Open a stream to copy the image contents to the WriteableBitmap's pixel buffer 
using (Stream stream = Scenario4WriteableBitmap.PixelBuffer.AsStream()) 
{ 
    await stream.WriteAsync(sourcePixels, 0, sourcePixels.Length); 
}     
1
On
InMemoryRandomAccessStream MStream = new InMemoryRandomAccessStream();
 MStream.Size = (ulong)wb.PixelBuffer.Length;

UpdateAndRender(sourcePixels, null, elapsedTimeUnits);

Random Rand = new Random();
Rand.NextBytes(sourcePixels);

BitmapEncoder enc = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, MStream);
enc.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)wb.PixelWidth, (uint)wb.PixelHeight, 200, 200, sourcePixels);
                                                                                await enc.FlushAsync();

wb.SetSource(MStream);

This is answer how to write into the WriteableBitmap, but still one question is open :

Is PixelBuffer.AsStream() obsolete ?