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.
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.