2017-03-16 27 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);  
}