2017-03-16 55 views
0

我从互联网加载图像源,我需要这个图像的主色。例如this图像,然后发现颜色的小偷,但我无法理解。如何使用色彩小偷与UWP

我使用这种方法,但我认为这是错误的。

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

我该怎么办?

回答

1

ColorThiefGetColor方法需要参数BitmapDecoder。但BitmapDecode不是通过您尝试的方式创建的。 BitmapDecoder可根据CreateAsync()方法由IRandomAccessStream创建,不能直接由Uri创建。所以你首先需要一个RandomAccessStream对象。这可以通过RandomAccessStreamReference.CreateFromUri(Uri)创建RandomAccessStreamReference,然后打开并阅读它来完成。使用ColorThief进行完整演示如下,您可以参考:

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