在Windows Phone 8.1 WinRT上共享屏幕截图不会在共享应用程序上附加图像

时间:2015-07-15 14:02:10

标签: c# windows-runtime windows-phone-8.1 sharing

我想分享我的应用的截图。屏幕截图保存在手机的图片库中,然后分享为StorageFile

问题是,图片未附加到共享应用程序。我已经确认屏幕截图已成功保存在手机的图片库中。

这是我的代码。我错过了什么?

    private async void askFacebook()
    {
        // Render some UI to a RenderTargetBitmap
        var renderTargetBitmap = new RenderTargetBitmap();
        await renderTargetBitmap.RenderAsync(this.gridRoot, (int)this.gridRoot.ActualWidth, (int)this.gridRoot.ActualHeight);

        // Get the pixel buffer and copy it into a WriteableBitmap
        var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
        var width = renderTargetBitmap.PixelWidth;
        var height = renderTargetBitmap.PixelHeight;
        var wbmp = await new WriteableBitmap(1, 1).FromPixelBuffer(pixelBuffer, width, height);

        imageToShare = await saveWriteableBitmapAsJpeg(wbmp, string.Format("{0}.jpg", getAppTitle()));
        DataTransferManager.ShowShareUI();
    }

    private string getAppTitle()
    {
        // Get the assembly with Reflection:
        Assembly assembly = typeof(App).GetTypeInfo().Assembly;

        // Get the custom attribute informations:
        var titleAttribute = assembly.CustomAttributes.Where(ca => ca.AttributeType == typeof(AssemblyTitleAttribute)).FirstOrDefault();

        // Now get the string value contained in the constructor:
        return titleAttribute.ConstructorArguments[0].Value.ToString();
    }

    private async Task<StorageFile> saveWriteableBitmapAsJpeg(WriteableBitmap bmp, string fileName)
    {
        // Create file in Pictures library and write jpeg to it
        var outputFile = await KnownFolders.PicturesLibrary.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
        using (var writeStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
        {
            await encodeWriteableBitmap(bmp, writeStream, BitmapEncoder.JpegEncoderId);
        }
        return outputFile;
    }

    private async Task encodeWriteableBitmap(WriteableBitmap bmp, IRandomAccessStream writeStream, Guid encoderId)
    {
        // Copy buffer to pixels
        byte[] pixels;
        using (var stream = bmp.PixelBuffer.AsStream())
        {
            pixels = new byte[(uint)stream.Length];
            await stream.ReadAsync(pixels, 0, pixels.Length);
        }

        // Encode pixels into stream
        var encoder = await BitmapEncoder.CreateAsync(encoderId, writeStream);
        var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;

        encoder.SetPixelData(BitmapPixelFormat.Bgra8, 
            BitmapAlphaMode.Premultiplied,
           (uint)bmp.PixelWidth, 
           (uint)bmp.PixelHeight,
           logicalDpi, 
           logicalDpi, 
           pixels);

        await encoder.FlushAsync();
    }


    private void ShareImageHandler(DataTransferManager sender,  DataRequestedEventArgs e)
    {
        DataRequest request = e.Request;
        request.Data.Properties.Title = "Ask Social Media";
        request.Data.Properties.Description = "Do you know the answer to this question?";

        // Because we are making async calls in the DataRequested event handler,
        //  we need to get the deferral first.
        DataRequestDeferral deferral = request.GetDeferral();

        // Make sure we always call Complete on the deferral.
        try
        {
            request.Data.SetBitmap(RandomAccessStreamReference.CreateFromFile(imageToShare));
        }
        finally
        {
            deferral.Complete();
        }
    }

1 个答案:

答案 0 :(得分:0)

显然对于Windows Phone,图像需要是StorageItem,因为org.springframework.web.servlet.view.tiles3.TilesViewResolver方法仅适用于Windows 8.x

对于Windows手机,而不是

spring-webmvc-3.2.0.RELEASE

我创建了一个存储项,并使用SetBitmap进行共享。它适用于本机Windows Phone应用程序,如电子邮件,OneNote,但我没有测试它在Facebook,Twitter等上分享。

request.Data.SetBitmap(RandomAccessStreamReference.CreateFromFile(imageToShare));