所以我试图获得IRandomAccessStreamWithContentType但不能,因为我得到了异常:
"This IRandomAccessStream does not support the CloneStream method because it requires cloning and this stream does not support cloning."
这发生在以下代码的最后一行:
PixelDataProvider pix = await decoder.GetPixelDataAsync(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Straight,
transform,
ExifOrientationMode.IgnoreExifOrientation,
ColorManagementMode.ColorManageToSRgb);
byte[] pixels = pix.DetachPixelData();
Stream pixStream = cropBmp.PixelBuffer.AsStream();
pixStream.Write(pixels, 0, (int)(width * height * 4));
IRandomAccessStream iStream= pixStream.AsRandomAccessStream(); //dafaq with streams
RandomAccessStreamReference iReferenceStream= RandomAccessStreamReference.CreateFromStream(iStream);
IRandomAccessStreamWithContentType newStream = await iReferenceStream.OpenReadAsync();
有没有解决方法或其他什么?
修改1
我也试过这种方式,仍然无法正常工作。 (但现在我得到的不是Clone失败了)
InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
await ras.WriteAsync(pixels.AsBuffer());
RandomAccessStreamReference iReferenceStream = RandomAccessStreamReference.CreateFromStream(ras);
IRandomAccessStreamWithContentType newStream = await iReferenceStream.OpenReadAsync();
答案 0 :(得分:0)
从您的代码中,您似乎使用了BitmapDecoder
。 BitmapDecoder
提供对位图容器数据的读访问以及来自第一帧的数据。
我们应该能够为编码器创建一个新的InMemoryRandomAccessStream
来写入并调用BitmapEncoder.CreateForTranscodingAsync
,传入内存中的流和解码器对象。
例如:
Windows.Storage.Streams.IRandomAccessStream random = await Windows.Storage.Streams.RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/sunset.jpg")).OpenReadAsync();
Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(random);
Windows.Graphics.Imaging.PixelDataProvider pixelData = await decoder.GetPixelDataAsync();
byte[] buffer = pixelData.DetachPixelData();
InMemoryRandomAccessStream inMemoryRandomAccessStream = new InMemoryRandomAccessStream();
BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(inMemoryRandomAccessStream, decoder);
encoder.BitmapTransform.ScaledWidth = 320;
encoder.BitmapTransform.ScaledHeight = 240;
await encoder.FlushAsync();
inMemoryRandomAccessStream.Seek(0);
random.Seek(0);
BitmapImage bitmapImage = new BitmapImage();
RandomAccessStreamReference iReferenceStream = RandomAccessStreamReference.CreateFromStream(inMemoryRandomAccessStream);
IRandomAccessStreamWithContentType newStream = await iReferenceStream.OpenReadAsync();
bitmapImage.SetSource(newStream);
MyImage.Source = bitmapImage;