windows-8windows-runtimemicrosoft-metrowinrt-xamlwritablebitmap

WritableBitmap to RandomAccessStreamReference


i have next question:

I have made a screenshot using WinRT XAML Toolkit (code1)

Now i want to attach this WritableBitmap to email (code2)

CODE 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
            }

CODE 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;

This means i need to firstly save image to local storage, and then read it and attach it.

The question:

1) How to save WritableBitmap to applications local storage?

2) Maybe there is a way not to save image to attach it?


Solution

  • UPDATE

    One more way using WriteableBitmapEx & MemoryRandomAccessStream

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

    Try this to save WriteableBitmap To 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;
        }
    }