正如@Clemens说,我们应该能够使用WriteableBitmap
。我们可以通过BitmapDecoder.PixelWidth
和BitmapDecoder.PixelHeight
属性获得宽度和高度。然后我们可以使用WriteableBitmap.PixelBuffer
将字节数组设置为WriteableBitmap
。
PixelBuffer不能直接写入,但是,您可以使用语言特定的技术访问缓冲区并更改其内容。 要从C#或Microsoft Visual Basic中访问像素内容,可以使用AsStream扩展方法以流的形式访问基础缓冲区。
欲了解更多信息,请参阅WriteableBitmap.PixelBuffer
的备注。
例如:
IRandomAccessStream random = await RandomAccessStreamReference.CreateFromUri(ImageWhite.UriSource).OpenReadAsync();
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(random);
PixelDataProvider pixelData = await decoder.GetPixelDataAsync();
var PixelArray = pixelData.DetachPixelData();
WriteableBitmap bitmap = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
await bitmap.PixelBuffer.AsStream().WriteAsync(PixelArray, 0, PixelArray.Length);
MyImage.Source = bitmap;
更新:
转换的WriteableBitmap
到BitmapImage
,我们应该能够到流从WriteableBitmap
编码。
例如:
InMemoryRandomAccessStream inMemoryRandomAccessStream = new InMemoryRandomAccessStream();
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, inMemoryRandomAccessStream);
Stream pixelStream = bitmap.PixelBuffer.AsStream();
byte[] pixels = new byte[pixelStream.Length];
await pixelStream.ReadAsync(pixels, 0, pixels.Length);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, 96.0, 96.0, pixels);
await encoder.FlushAsync();
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(inMemoryRandomAccessStream);
MyImage.Source = bitmapImage;
'BitmapImage.SetSourceAsync'不接受原始像素缓冲器,但只有一个编码的位图的帧,例如一个PNG或JPEG。您可以改用WriteableBitmap。 – Clemens
如何获取字节数组?如果你从中得到它,我们应该能够通过你的方法得到它。 –
嗨Jayden顾,感谢您的评论。这是我如何得到我的数组: – moh67