我有下一个问题:
我使用WinRT XAML Toolkit(code1)
制作了截图现在我想将此WritableBitmap附加到电子邮件(code2)
代码1
WriteableBitmap wb = null;
var start = DateTime.Now;
const int count = 1;
for (int i = 0; i < count; i++)
{
wb = await WriteableBitmapRenderExtensions.Render(this);
}
var end = DateTime.Now;
var duration = end - start;
var renderInS = duration.TotalMilliseconds / count;
if (renderInS > 0)
{
test.Source = wb;
// HERE MUST BE CODE INCLUDED
}
代码2
DataPackage requestData = request.Data;
requestData.Properties.Title = TitleInputBox.Text;
requestData.Properties.Description = DescriptionInputBox.Text; // The description is optional.
// It's recommended to use both SetBitmap and SetStorageItems for sharing a single image
// since the target app may only support one or the other.
List<IStorageItem> imageItems = new List<IStorageItem>();
imageItems.Add(this.imageFile);
requestData.SetStorageItems(imageItems);
RandomAccessStreamReference imageStreamRef = RandomAccessStreamReference.CreateFromFile(this.imageFile);
requestData.Properties.Thumbnail = imageStreamRef;
requestData.SetBitmap(imageStreamRef);
succeeded = true;
这意味着我首先需要将图像保存到本地存储,然后读取并附加它。
问题:
1)如何将WritableBitmap保存到应用程序本地存储?
2)也许有一种方法可以不保存图像来附加它?
答案 0 :(得分:2)
更新
使用WriteableBitmapEx
&amp;的另一种方法MemoryRandomAccessStream
MemoryStream stream = new MemoryStream(MyWriteableBitmap.ToByteArray());
var randomAccessStream = new MemoryRandomAccessStream(stream);
DataPackage requestData = args.Request.Data;
RandomAccessStreamReference imageStreamRef = RandomAccessStreamReference.CreateFromStream(randomAccessStream);
尝试将WriteableBitmap保存到StorageFile
private async Task<StorageFile> WriteableBitmapToStorageFile(WriteableBitmap writeableBitmap)
{
var picker = new FileSavePicker();
picker.FileTypeChoices.Add("JPEG Image", new string[] { ".jpg" });
StorageFile file = await picker.PickSaveFileAsync();
if (file != null && writeableBitmap != null)
{
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
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 file;
}
else
{
return null;
}
}