如何在 UWP 中使用 Color Thief

How to Use Color Thief With UWP

我从互联网加载图像源,我需要这个图像的主色。例如this图片,然后发现色贼,但我看不懂。

我在用这个方法,但我认为这是错误的。

BitmapDecoder BMD = new BitmapDecoder("https://yt3.ggpht.com/-cYK4gMKhvV0/AAAAAAAAAAI/AAAAAAAAAAA/8znlvBw-Wos/s100-c-k-no-mo-rj-c0xffffff/photo.jpg");
var colorThief = new ColorThief();
await colorThief.GetColor(BMD);

我该怎么做?

没错ColorThief requires a BitmapDecoder parameter. But BitmapDecode is not created by the way you are trying. BitmapDecoder can be created by IRandomAccessStream according to CreateAsync() method, cannot be created directly by a Uri. So you need a RandomAccessStream object firstly. This can be done by creating a RandomAccessStreamReference by RandomAccessStreamReference.CreateFromUri(Uri)GetColor方法,然后打开阅读。使用ColorThief的完整demo如下,大家可以参考:

Uri imageUri = new Uri("https://yt3.ggpht.com/-cYK4gMKhvV0/AAAAAAAAAAI/AAAAAAAAAAA/8znlvBw-Wos/s100-c-k-no-mo-rj-c0xffffff/photo.jpg");
RandomAccessStreamReference random = RandomAccessStreamReference.CreateFromUri(imageUri);
using (IRandomAccessStream stream = await random.OpenReadAsync())   
{
    //Create a decoder for the image
    var decoder = await BitmapDecoder.CreateAsync(stream);
    var colorThief = new ColorThief();
    var color = await colorThief.GetColor(decoder);       
}