0

I am Setting thumbnail of SMTC control from stream via SystemMediaTransportControlsDisplayUpdater, but it is not working

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

Same is working if i use Url to create random stream.

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

I am merging two images and i want to assign that image as thumbnai to SMTC. To merge images below code i am using.

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

What exactly wrong with merging or setting thumbnail to SMTC? Thanks

Pavan Tiwari
  • 3,077
  • 3
  • 31
  • 71
  • When I call `writeableBmp.FromContent(imgUrl1);` method. it always return null, what's result in your side ? – Nico Zhu Apr 30 '19 at 03:21
  • argument for FromContent should be Uri object and Uri should not be an http request url. It should be local project asset path. otherwise it will throw an exception in different thread and you will get null always . – Pavan Tiwari Apr 30 '19 at 06:16
  • I tried `RandomAccessStreamReference.CreateFromStream` method it works well, I could not reproduce your issue, Could share a sample project that could reproduce this issue ? – Nico Zhu Apr 30 '19 at 06:18
  • If you used local assets path, httpClient will not accept this url argument, right? – Nico Zhu Apr 30 '19 at 06:22
  • For image1 i am using http path and for image2 i am using local path. That's why you can see in code one place i am using FromStream method and in other place i am using FromContent method. – Pavan Tiwari Apr 30 '19 at 17:44

1 Answers1

0

stream via SystemMediaTransportControlsDisplayUpdater, but it is not working

I could reproduce your issue, I will report this, currently there is a workaround that converter WriteableBitmap to StorageFile then pass file argument to RandomAccessStreamReference.CreateFromFile method. You could refer the following code,

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
}
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36