我目前正在为 Windows Store 编写一个小型图像编辑应用程序,我想在其中使用共享魅力来编辑图像。由于某些应用程序只接受存储文件而不接受位图(如默认邮件应用程序),我想以两种方式提供图像,如下所述:http: //www.jeffblankenburg.com/2012/11/07/31- days-of-windows-8-day-7-share-contract 但是,我不知道如何从 WrieableBitmap 创建 StorageFile。
也许您可以帮助我完成下面的代码。
// Share Image
async void dtm_DataRequested(DataTransferManager sender, DataRequestedEventArgs e)
{
DataRequestDeferral deferral = e.Request.GetDeferral();
DataPackage requestData = e.Request.Data;
requestData.Properties.Title = "My Image";
requestData.Properties.Description = "Created using a Windows Store App.";
// This stream is to create a bitmap image later
IRandomAccessStream stream = new InMemoryRandomAccessStream();
// Determin which type of BitmapEncoder we should create
Guid encoderId = BitmapEncoder.JpegEncoderId;
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(encoderId, stream);
Stream pixelStream = theImage.PixelBuffer.AsStream();
byte[] pixels = new byte[pixelStream.Length];
await pixelStream.ReadAsync(pixels, 0, pixels.Length);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)theImage.PixelWidth, (uint)theImage.PixelHeight, 96.0, 96.0, pixels);
requestData.SetBitmap(RandomAccessStreamReference.CreateFromStream(stream));
// What goes here?
StorageFile storageFileImage =
List<IStorageItem> files = new List<IStorageItem>();
files.Add(storageFileImage);
requestData.SetStorageItems(files);
requestData.Properties.Thumbnail = RandomAccessStreamReference.CreateFromStream(stream);
await encoder.FlushAsync();
deferral.Complete();
}