Using Data Transfer Manager to share an image on windows phone 8.1

Jul 21, 2014
1
0
0
I want to share an image in my windows phone 8.1 application using DataTransferManager.
For this I use my windows 8.1 app code for sharing using DataTransfer which works fine on Windows 8.1 but it doesnt work on WP8.1.

Here is my approach, I am converting my canvas into an image using RenderBitmap class then creating a RandomAccessStream of the bitmap to use in SetBitmap method.

args.Request.Data.SetBitmap(RandomAccessStreamReference.CreateFromStream(stream));

protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
DataTransferManager.GetForCurrentView().DataRequested+= MainPage_DataRequested;
}

private async void MainPage_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
var deferral = args.Request.GetDeferral();
var bitmap = new RenderTargetBitmap();
await bitmap.RenderAsync(canvas);

// 1. Get the pixels
IBuffer pixelBuffer = await bitmap.GetPixelsAsync();
byte[] pixels = pixelBuffer.ToArray();

// 2. Write the pixels to a InMemoryRandomAccessStream
var stream = new InMemoryRandomAccessStream();
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, stream);

encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, 96, 96,
pixels);

await encoder.FlushAsync();
stream.Seek(0);

// 3. Share it
args.Request.Data.SetBitmap(RandomAccessStreamReference.CreateFromStream(stream));
args.Request.Data.Properties.Description = "description";
args.Request.Data.Properties.Title = "title";
deferral.Complete();
}

This does not give any error but the image is not seen in the sharing apps like Messaging,OneNote etc.This code works fine in windows 8.1 app.Please need help stuck here for a long time.
 
Hi and welcome to WPCentral.

I've moved your thread to the Developers Corner forum, since other developers will be able to assist you with coding better than regular users.
 

Members online

Forum statistics

Threads
335,627
Messages
2,258,633
Members
428,741
Latest member
Hewill