我有一个图像处理windows 10应用程序。我正在对该图像应用滤镜并在图像元素上显示它。这就是我如何应用过滤器并将其设置为MainImage元素的源。
ProcessImage processImage = new ProcessImage(sourcePixels, width, height);
byte[] blurEffect = processImage.BlurEffect(width, height);
WriteableBitmap blurImage = new WriteableBitmap((int)width, (int)height);
using (Stream stream = blurImage.PixelBuffer.AsStream())
{
await stream.WriteAsync(blurEffect, 0, blurEffect.Length);
MainImage.Source = blurImage;
}
到目前为止,我已将WriteableBitmap图像设置为源。现在我想使用DataTransferManager的Data request事件共享此图像,如图所示
dataTransferManager = DataTransferManager.GetForCurrentView();
dataTransferManager.DataRequested += DataTransferManager_DataRequested;
包含此代码的事件正文
DataPackage dataPackage = args.Request.Data;
dataPackage.Properties.Title = "App Name";
dataPackage.Properties.Description = "My description";
dataPackage.SetBitmap();
在分享按钮点击事件中,我正在调用showhareUI,就像这样
DataTransferManager.ShowShareUI();
我正在尝试使用上面的第四行分享图像,即SetBitmap方法,但问题是此方法需要 RandomAccessStreamReference 值,并且我有一个类型为writeablebitmap的过滤图像。我怎样才能完成这件事呢?
答案 0 :(得分:1)
您可以将WriteableBitmap写入InMemoryRandomAccessStream
我无法访问我的开发机器,所以我无法对其进行测试,但这是一个快速的示例:
private async Task<IRandomAccessStream> Convert(WriteableBitmap writeableBitmap)
{
var stream = new InMemoryRandomAccessStream();
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
Stream pixelStream = writeableBitmap.PixelBuffer.AsStream();
byte[] pixels = new byte[pixelStream.Length];
await pixelStream.ReadAsync(pixels, 0, pixels.Length);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)writeableBitmap.PixelWidth, (uint)writeableBitmap.PixelHeight, 96.0, 96.0, pixels);
await encoder.FlushAsync();
return stream;
}
由于dataPackage.SetBitmap()
需要一个RandomAccessStreamReference
对象,您需要根据上述方法返回的IRandomAccessStream
获取一个对象。幸运的是,这很简单:
var streamRef = RandomAccessStreamReference.CreateFromStream(stream)
希望有效。