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

标签 c# ios xamarin xamarin.forms

我正在使用 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/

相关文章:

c# - 如何使用 C# 读取 Microsoft Edge 历史记录

c# - .NET 多线程、 volatile 和内存模型

c# - 将数据异步加载到数据网格中

ios - 合并具有相同父类(super class)的 Swift 数组

android - 从布局中移除子项

javascript - 使用来自 JS xamarin 的参数调用 C# 方法

c# - RegEx 之后用特定文本捕获执行时间

ios - 如何根据声明顺序对 UITableView header 进行排序

iOS 核心数据更新 : changes not saved

android - Xamarin Forms Custom Renderer for Android 不显示