0

我有下一个问题:

我使用 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)也许有办法不保存图像来附加它?

4

1 回答 1

2

更新

使用WriteableBitmapEx&的另一种方法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;
    }
}
于 2013-09-30T08:33:09.867 回答