关于windows 8:WriteableBitmap到RandomAccessStreamReference

WritableBitmap to RandomAccessStreamReference

我还有一个问题:

我使用Winrt XAML工具包(代码1)进行了截图

现在我想将这个可写位图附加到电子邮件(代码2)

代码1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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)如何将可写位图保存到应用程序本地存储?

2)也许有办法不保存图像来附加它?


更新

使用WriteableBitmapExMemoryRandomAccessStream的另一种方法

1
2
3
4
MemoryStream stream = new MemoryStream(MyWriteableBitmap.ToByteArray());
var randomAccessStream = new MemoryRandomAccessStream(stream);
DataPackage requestData = args.Request.Data;
RandomAccessStreamReference imageStreamRef = RandomAccessStreamReference.CreateFromStream(randomAccessStream);

尝试将可写位图保存到存储文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;
    }
}