C# UWP 从 System.IO.Stream 转换为 Windows.Storage.Streams.IRandomAccessStreamWithContentType
C# UWP Converting from System.IO.Stream to Windows.Storage.Streams.IRandomAccessStreamWithContentType
所以我试图获取 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
这个方法我也试过了,还是不行。 (但现在我得到 null 而不是克隆失败)
InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
await ras.WriteAsync(pixels.AsBuffer());
RandomAccessStreamReference iReferenceStream = RandomAccessStreamReference.CreateFromStream(ras);
IRandomAccessStreamWithContentType newStream = await iReferenceStream.OpenReadAsync();
从你的代码来看,你似乎使用了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;
所以我试图获取 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
这个方法我也试过了,还是不行。 (但现在我得到 null 而不是克隆失败)
InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
await ras.WriteAsync(pixels.AsBuffer());
RandomAccessStreamReference iReferenceStream = RandomAccessStreamReference.CreateFromStream(ras);
IRandomAccessStreamWithContentType newStream = await iReferenceStream.OpenReadAsync();
从你的代码来看,你似乎使用了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;