Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

c# - How to Use Color Thief With UWP

I loading images source from internet and I need this images dominant color. Forexample this image and then found color thief but I cant understand.

I using this method but I think it's wrong.

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

How can I do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is right that the GetColor method of 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), and then open and read it. A complete demo by using the ColorThief is as follows you can reference:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...