Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing render to bitmap image in windows phone 8.1

I want to share my canvas as image in windows phone 8.1.For this I first convert my canvas to an image then share it. I tried my windows 8.1 code .No errors occur but image is not there in share source app only description and title appears.

Here is the code:

private async void DataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs e)
        {
            e.Request.Data.Properties.Title = "My app";
            e.Request.Data.Properties.Description = "app description";

            DataRequest request = e.Request;

            // Request deferral to wait for async calls
            DataRequestDeferral deferral = request.GetDeferral();

            // XAML objects can only be accessed on the UI thread, and the call may come in on a background thread
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
            {
                try
                {

                    RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
                    InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
                    // Render to an image at the current system scale and retrieve pixel contents
                    await renderTargetBitmap.RenderAsync(SavedCanvas);
                    var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();

                    // Encode image to an in-memory stream
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);

                    encoder.SetPixelData(
                        BitmapPixelFormat.Bgra8,
                        BitmapAlphaMode.Ignore,
                        (uint)renderTargetBitmap.PixelWidth,
                        (uint)renderTargetBitmap.PixelHeight,
                        DisplayInformation.GetForCurrentView().LogicalDpi,
                        DisplayInformation.GetForCurrentView().LogicalDpi,
                        pixelBuffer.ToArray());

                    await encoder.FlushAsync();


                    request.Data.Properties.Thumbnail = RandomAccessStreamReference.CreateFromStream(stream);

                    //  e.Request.Data.Properties.Thumbnail=(RandomAccessStreamReference.CreateFromStream(stream));
                    // Set content of the DataProviderRequest to the encoded image in memory
                    request.Data.SetBitmap(RandomAccessStreamReference.CreateFromStream(stream));
                }
                finally
                {
                    deferral.Complete();

                }
            });

        }

enter image description here

enter image description here

This works fine in windows 8.1 , I think it should work fine here too.Image not seen in sharing apps like messaging,OneNote etc.

Need help.Thanks.

like image 514
Ghazanfar Khan Avatar asked Jul 19 '14 08:07

Ghazanfar Khan


1 Answers

You are passing bitmap to an app which doesn't support bitmaps then the bitmap will be ignored. Sending a bitmap file instead is commonly needed. You can save your file out and then load that StorageFile or you can create an in memory StorageFile. For testing purposes I'd save the file to a StorageFile, make sure that the file can be shown correctly in the app, and then make sure it works correctly when sharing. This sample might be helpful. http://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597

like image 153
user3090763 Avatar answered Oct 21 '22 15:10

user3090763