设置SystemMediaTransportControlsDisplayUpdater的缩略图

时间:2019-04-30 01:18:24

标签: c# uwp png writeablebitmap writeablebitmapex

我正在通过SystemMediaTransportControlsDisplayUpdater从流中设置SMTC控件的缩略图,但不起作用

    var response = await new HttpClient().GetAsync(imgUrl);
systemMediaTransportControlsDisplayUpdater.Thumbnail = RandomAccessStreamReference.CreateFromStream((await response.Content.ReadAsStreamAsync()).AsRandomAccessStream());

如果我使用Url来创建随机流,同样可以正常工作。

systemMediaTransportControlsDisplayUpdater.Thumbnail = RandomAccessStreamReference.CreateFromUri(new Uri(imgUrl));

我正在合并两个图像,我想将该图像作为Thumbnai分配给SMTC。要合并我正在使用的代码下方的图像。

 var response = await httpClient.GetAsync(imgUrl);
                var writeableBmp = new WriteableBitmap(1, 1);
                var image1 = await writeableBmp.FromStream(await response.Content.ReadAsStreamAsync());
                var image2 = await writeableBmp.FromContent(imgUrl1);

                image1.Blit(new Rect(0, 0, image1.PixelWidth, image1.PixelHeight), image2, new Rect(0, 0, image2.PixelWidth, image2.PixelHeight));
                var randomAccessStream = image1.PixelBuffer.AsStream().AsRandomAccessStream();
systemMediaTransportControlsDisplayUpdater.Thumbnail = 
 = RandomAccessStreamReference.CreateFromStream(randomAccessStream );

将缩略图合并或设置为SMTC到底有什么问题? 谢谢

1 个答案:

答案 0 :(得分:0)

  通过SystemMediaTransportControlsDisplayUpdater

流,但不起作用

我可以重现您的问题,我将向您报告,目前,有一种解决方法是将转换器WriteableBitmap转换为StorageFile,然后将文件参数传递给RandomAccessStreamReference.CreateFromFile方法。您可以参考以下代码,

private async void MediaPlayer_MediaOpened(MediaPlayer sender, object args)
{
    await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
    {
        var imgUrl = new Uri("https://image.diyidan.net/post/2015/10/27/LILeAOb3BF8qR8Uz.png");
        var imgUrl1 = new Uri("ms-appx:///Assets/test.png");
        var httpclient = new HttpClient();
        var response = await httpclient.GetAsync(imgUrl);

        var writeableBmp = new WriteableBitmap(1, 1);
        var image1 = await writeableBmp.FromStream(await response.Content.ReadAsStreamAsync());
        var image2 = await writeableBmp.FromContent(imgUrl1);


        image1.Blit(new Rect(0, 0, image1.PixelWidth, image1.PixelHeight), image2, new Rect(0, 0, image2.PixelWidth, image2.PixelHeight));

        var file = await WriteableBitmapToStorageFile(image1, FileFormat.Png);

        SystemMediaTransportControlsDisplayUpdater updater = sender.SystemMediaTransportControls.DisplayUpdater;
        updater.Thumbnail = RandomAccessStreamReference.CreateFromFile(file);
        updater.Update();
    });

}
private async Task<StorageFile> WriteableBitmapToStorageFile(WriteableBitmap WB, FileFormat fileFormat)
{
    string FileName = "YourFile.";
    Guid BitmapEncoderGuid = BitmapEncoder.JpegEncoderId;
    switch (fileFormat)
    {
        case FileFormat.Jpeg:
            FileName += "jpeg";
            BitmapEncoderGuid = BitmapEncoder.JpegEncoderId;
            break;
        case FileFormat.Png:
            FileName += "png";
            BitmapEncoderGuid = BitmapEncoder.PngEncoderId;
            break;
        case FileFormat.Bmp:
            FileName += "bmp";
            BitmapEncoderGuid = BitmapEncoder.BmpEncoderId;
            break;
        case FileFormat.Tiff:
            FileName += "tiff";
            BitmapEncoderGuid = BitmapEncoder.TiffEncoderId;
            break;
        case FileFormat.Gif:
            FileName += "gif";
            BitmapEncoderGuid = BitmapEncoder.GifEncoderId;
            break;
    }
    var file = await Windows.Storage.ApplicationData.Current.TemporaryFolder.CreateFileAsync(FileName, CreationCollisionOption.GenerateUniqueName);
    using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
    {
        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoderGuid, stream);
        Stream pixelStream = WB.PixelBuffer.AsStream();
        byte[] pixels = new byte[pixelStream.Length];
        await pixelStream.ReadAsync(pixels, 0, pixels.Length);
        encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)WB.PixelWidth, (uint)WB.PixelHeight,
            96.0,
            96.0,
            pixels);
        await encoder.FlushAsync();
    }
    return file;
}
private enum FileFormat
{
    Jpeg,
    Png,
    Bmp,
    Tiff,
    Gif
}