Using SetBitmap with the DataPackage does not work for some apps

fangfang wu 91 Reputation points Microsoft Employee
2019-10-30T06:52:33.443+00:00

I am trying to implement the share app functionality using the DataRequest. I am creating a RenderTargetBitmap from a Grid shown on my app, saving it as a PNG file and then loading the file into my DataPackage with the SetBitmap method.

This is working but I am expecting apps like the Facebook and Instagram apps did not show in my list of apps to share, all I get is Mail and Onenote apps. Can anybody tell me if I am doing anything wrong or missing a step?

Here is my code that creates the request:

	        var deferral = args.Request.GetDeferral();  
            DataRequest request = args.Request;  
  
            request.Data.Properties.Title = "Quote Collection";  
            request.Data.Properties.Description = "Share your quote";  
              
            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();  
  
            FlipViewItem item = (FlipViewItem)flipView.ContainerFromItem(flipView.SelectedItem);  
  
            Grid grid = HelperMethods.FindElementByName(item, "QuoteGrid");  
            await renderTargetBitmap.RenderAsync(grid);  
  
            var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();  
            var pixels = pixelBuffer.ToArray();  
            var displayInformation = DisplayInformation.GetForCurrentView();  
            var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("quoteImage" + ".png", CreationCollisionOption.ReplaceExisting);  
            using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))  
            {  
                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);  
                encoder.SetPixelData(BitmapPixelFormat.Bgra8,  
                                     BitmapAlphaMode.Premultiplied,  
                                     (uint)renderTargetBitmap.PixelWidth,  
                                     (uint)renderTargetBitmap.PixelHeight,  
                                     displayInformation.RawDpiX,  
                                     displayInformation.RawDpiY,  
                                     pixels);  
                await encoder.FlushAsync();  
            }  
  
            var qfile = await ApplicationData.Current.LocalFolder.GetFileAsync("quoteImage.png");  
  
            request.Data.SetBitmap(RandomAccessStreamReference.CreateFromFile(file));  
  
            deferral.Complete();  
  
Universal Windows Platform (UWP)
0 comments No comments
{count} vote

Accepted answer
  1. Roy Li - MSFT 31,521 Reputation points Microsoft Vendor
    2019-10-30T07:46:36.43+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    This is working but I am expecting apps like the Facebook and Instagram apps did not show in my list of apps to share, all I get is Mail and Onenote apps.

    Based on this official document the recommended type for sharing an image is the StorageFile. Only apps which support receiving Bitmaps will show up if you share only a Bitmap. It is much more common for apps to receive image files than uncompressed Bitmaps, you can share both and let the target pick if Bitmaps make sense for your app.

    So please adding the request.Data.SetStorageItems in your code and it should work for you.

    Thanks.

    2 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more