在Windows Phone 8.1中将渲染共享到位图图像 [英] Sharing render to bitmap image in windows phone 8.1

查看:52
本文介绍了在Windows Phone 8.1中将渲染共享到位图图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Windows Phone 8.1中将画布作为图像共享.为此,我首先将画布转换为图像,然后共享.我尝试了Windows 8.1代码.没有错误发生,但共享源应用程序中没有图像,仅描述和标题出现了.

这是代码:

 私有异步void DataTransferManager_DataRequested(DataTransferManager sender,DataRequestedEventArgs e){e.Request.Data.Properties.Title =我的应用程序";e.Request.Data.Properties.Description =应用程序描述";DataRequest请求= e.Request;//请求延迟以等待异步调用DataRequestDeferral deferral = request.GetDeferral();//XAML对象只能在UI线程上访问,并且调用可能在后台线程上进行等待Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,async()=>{尝试{RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();InMemoryRandomAccessStream流=新的InMemoryRandomAccessStream();//以当前系统比例渲染到图像并检索像素内容等待renderTargetBitmap.RenderAsync(SavedCanvas);var pixelBuffer =等待renderTargetBitmap.GetPixelsAsync();//将图像编码为内存流varcoder =等待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());等待encoder.FlushAsync();request.Data.Properties.Thumbnail = RandomAccessStreamReference.CreateFromStream(stream);//e.Request.Data.Properties.Thumbnail =(RandomAccessStreamReference.CreateFromStream(stream));//将DataProviderRequest的内容设置为内存中的编码图像request.Data.SetBitmap(RandomAccessStreamReference.CreateFromStream(stream));}最后{deferral.Complete();}});} 

这在Windows 8.1中可以正常工作,我认为它在这里也可以正常工作.在共享应用程序(如消息传递,OneNote等)中看不到图像

需要帮助.谢谢.

解决方案

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

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

                }
            });

        }

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.

解决方案

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

这篇关于在Windows Phone 8.1中将渲染共享到位图图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆