gpt4 book ai didi

c# - Xamarin.Forms C# 查找图像或图像 byte[] 数组的主色

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:16:23 26 4
gpt4 key购买 nike

我正在使用 Xamarin.Forms 开发跨平台应用。使用 c# 和 Xamarin 查找图像主色的最佳方法是什么?我找到了一个 ios 方法:https://github.com/mxcl/UIImageAverageColor/blob/master/UIImage%2BAverageColor.m但似乎无法转换为等效的 c#。什么是好方法?对于我的 ios 实现,我可以使用 UIImage 或 byte[] 数组。

感谢您的帮助!

最佳答案

你可以试试这个(但它得到一个 Bitmap 对象,希望能有所帮助):

public static Color getDominantColor(Bitmap bmp)
{
//Used for tally
int red = 0;
int green = 0;
int blue = 0;

int acc = 0;

for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
Color tmpColor = bmp.GetPixel(x, y);

red += tmpColor.R;
green += tmpColor.G;
blue += tmpColor.B;

acc++;
}
}

//Calculate average
red /= acc;
green /= acc;
blue /= acc;

return Color.FromArgb(red, green, blue);
}

不要忘记使用:

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

编辑:

无法为您的 Byte[] 图像表示找到确切的解决方案,但我找到了这个:

public static byte[] ImageToByteArray(Image image)
{
ImageConverter myConverter = new ImageConverter();
return (byte[])myConverter.ConvertTo(image typeof(byte[]));
}

如您所见,上面的代码将 Image 转换为 Byte[]

干杯!

关于c# - Xamarin.Forms C# 查找图像或图像 byte[] 数组的主色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28123300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com