在Windows Phone 8.1中分享渲染为位图图像

11

我希望在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();

                }
            });

        }

输入图像描述

输入图像描述

这在 Windows 8.1 中可以正常工作,我认为在这里也应该可以。但是在分享应用程序中(如消息、OneNote 等)无法显示图像。需要帮助。谢谢。


非常感谢,伙计 :) 我以前是这样填充流的: await stream.WriteAsync(byteArray.AsBuffer()); 但每次我想将其用作 BitmapImage 时都会崩溃。 - Bassem Wissa
1个回答

6
如果您将位图传递给不支持位图的应用程序,则位图将被忽略。常见的情况是需要发送位图文件。 您可以将文件保存并加载该StorageFile,或者创建一个内存中的StorageFile。 为了测试目的,我建议将文件保存到StorageFile中,并确保在应用程序中正确显示该文件,然后确保在共享时它能够正常工作。 这个示例可能会有所帮助。 http://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接