在Windows Phone 8.1中共享渲染到位图图像

时间:2014-07-19 08:19:04

标签: c# windows-phone-8.1

我想在Windows Phone 8.1中将我的画布分享为图像。为此,我首先将画布转换为图像然后共享它。我尝试了我的Windows 8.1代码。没有出现错误但图片不在共享源应用程序中仅显示说明和标题。

以下是代码:

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

这在Windows 8.1中运行良好,我认为它在这里也可以正常工作。在分享应用程序(如消息,OneNote等)时看不到图像。

需要帮助。谢谢。

1 个答案:

答案 0 :(得分:6)

您正在将位图传递给不支持位图的应用程序,然后将忽略位图。通常需要发送位图文件。 您可以保存文件,然后加载StorageFile,也可以创建内存StorageFile。 出于测试目的,我将文件保存到StorageFile,确保文件可以在应用程序中正确显示,然后确保它在共享时正常工作。 此示例可能会有所帮助。 http://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597