locked
[UWP]Get thumbnail file from another file. RRS feed

  • Question

  • I'm trying to get a image file from a thumbnail. I want to use this to display an average color over a surface.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Windows.Graphics.Imaging;
    using Windows.Storage;
    using Windows.Storage.FileProperties;
    using Windows.UI;
    using Windows.UI.Xaml.Media.Imaging;
    
    namespace Ultamedia.Music
    {
        class SharedData
        {
            public static MusicProperties musicProperties
            {
                get
                {
                    return file.Properties.GetMusicPropertiesAsync().GetResults();
                }
            }
            public static Windows.Storage.StorageFile file;
    
            public static Color iconAverageColor
            {
                get
                {
                    var decoder =  BitmapDecoder.CreateAsync(file.GetScaledImageAsThumbnailAsync(ThumbnailMode.MusicView, 200).GetResults()).GetResults();
                    //Create a transform to get a 1x1 image
                    var myTransform = new BitmapTransform { ScaledHeight = 1, ScaledWidth = 1 };
    
                    //Get the pixel provider
                    var pixels = decoder.GetPixelDataAsync(
                        BitmapPixelFormat.Rgba8,
                        BitmapAlphaMode.Ignore,
                        myTransform,
                        ExifOrientationMode.IgnoreExifOrientation,
                        ColorManagementMode.DoNotColorManage).GetResults();
    
                    //Get the bytes of the 1x1 scaled image
                    var bytes = pixels.DetachPixelData();
    
                    //read the color 
                    var myDominantColor = Color.FromArgb(255, bytes[0], bytes[1], bytes[2]);
                    return myDominantColor;
                }
            }
        }
    }

    it fails on the "var decoder" line in "iconAverageColor". How can i fix this?

    Thursday, January 18, 2018 9:27 PM

Answers

  • Hi GiantNuker,

    I could reproduce the problem you said, it fails when creating decoder. After test, I found a solution to your problem. I changed the static property “iconAverageColor” to a static method and use await instead of task.getresult(). Then the app could run correctly.

    Here is my code snippet:

            public async static Task<Color> geticonAverageColor ()
            {
                StorageItemThumbnail thumbnail = await file.GetScaledImageAsThumbnailAsync(ThumbnailMode.MusicView, 200);
                var decoder = await BitmapDecoder.CreateAsync(thumbnail);
    
                //Create a transform to get a 1x1 image
                var myTransform = new BitmapTransform { ScaledHeight = 1, ScaledWidth = 1 };
    
                //Get the pixel provider
                var pixels = await decoder.GetPixelDataAsync(
                    BitmapPixelFormat.Rgba8,
                    BitmapAlphaMode.Ignore,
                    myTransform,
                    ExifOrientationMode.IgnoreExifOrientation,
                    ColorManagementMode.DoNotColorManage);
    
                //Get the bytes of the 1x1 scaled image
                var bytes = pixels.DetachPixelData();
    
                //read the color 
                Color myDominantColor = Color.FromArgb(255, bytes[0], bytes[1], bytes[2]);
                return myDominantColor;
    
            }

    And I call the method like this:

                //Color color = SharedData.iconAverageColor;
                Color color = await SharedData.geticonAverageColor();

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, January 19, 2018 7:05 AM

All replies

  • Hi GiantNuker,

    I could reproduce the problem you said, it fails when creating decoder. After test, I found a solution to your problem. I changed the static property “iconAverageColor” to a static method and use await instead of task.getresult(). Then the app could run correctly.

    Here is my code snippet:

            public async static Task<Color> geticonAverageColor ()
            {
                StorageItemThumbnail thumbnail = await file.GetScaledImageAsThumbnailAsync(ThumbnailMode.MusicView, 200);
                var decoder = await BitmapDecoder.CreateAsync(thumbnail);
    
                //Create a transform to get a 1x1 image
                var myTransform = new BitmapTransform { ScaledHeight = 1, ScaledWidth = 1 };
    
                //Get the pixel provider
                var pixels = await decoder.GetPixelDataAsync(
                    BitmapPixelFormat.Rgba8,
                    BitmapAlphaMode.Ignore,
                    myTransform,
                    ExifOrientationMode.IgnoreExifOrientation,
                    ColorManagementMode.DoNotColorManage);
    
                //Get the bytes of the 1x1 scaled image
                var bytes = pixels.DetachPixelData();
    
                //read the color 
                Color myDominantColor = Color.FromArgb(255, bytes[0], bytes[1], bytes[2]);
                return myDominantColor;
    
            }

    And I call the method like this:

                //Color color = SharedData.iconAverageColor;
                Color color = await SharedData.geticonAverageColor();

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, January 19, 2018 7:05 AM
  • THank you
    Saturday, January 20, 2018 12:53 AM
  • @GiantNuker,

    I have updated my reply. Please check it.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Saturday, January 20, 2018 8:17 AM