question

fangfangwu avatar image
fangfangwu asked ·

Using SetBitmap with the DataPackage does not work for some apps

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();


windows-uwpwindows-uwp-xamlwindows-uwp-runtime
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RoyLi-MSFT avatar image
RoyLi-MSFT answered ·

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.


Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.