3

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.

   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 works fine in windows 8.1 how ever image not attaching in sharing apps like Messaging,OneNote etc in windows phone 8.1

enter image description here

enter image description here

Need help , Stuck here for a long time.

Ghazanfar Khan
  • 3,648
  • 8
  • 44
  • 89
  • I think you should read this: http://stackoverflow.com/questions/13589448/sharing-from-windows-phone-8 It is the best answer for you right now. – meneses.pt Jul 22 '14 at 09:30
  • 1
    Have a look at this answer http://stackoverflow.com/questions/24838278/sharing-render-to-bitmap-image-in-windows-phone-8-1 – user3090763 Dec 23 '14 at 13:14
  • Finally solved it http://stackoverflow.com/questions/24838278/sharing-render-to-bitmap-image-in-windows-phone-8-1 – Ghazanfar Khan Dec 23 '14 at 13:16

0 Answers0