问题描述
我正在加载来自互联网的图像源,我需要这些图像占主导地位的颜色。例如图片,然后发现彩色小偷,但我听不懂。
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);
我该怎么办?
推荐答案
需要参数。但是 BitmapDecode
不是按照您尝试的方式创建的。 BitmapDecoder
可以由根据方法,不能由Uri直接创建。因此,您首先需要一个 RandomAccessStream
对象。可以通过RandomAccessStreamReference 来完成.randomaccessstreamreference#Windows_Storage_Streams_RandomAccessStreamReference_CreateFromUri_Windows_Foundation_Uri_ rel = nofollow noreferrer> RandomAccessStreamReference.CreateFromUri(Uri)
,然后打开并读取它。使用ColorThief的完整演示如下,您可以参考:
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);
}
这篇关于如何在UWP中使用彩色小偷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!