Xaml 如何从Xamarin.Forms for iOS中的图像中获取最常用的像素颜色?

Xaml 如何从Xamarin.Forms for iOS中的图像中获取最常用的像素颜色?,xaml,image-processing,xamarin.forms,xamarin.ios,Xaml,Image Processing,Xamarin.forms,Xamarin.ios,我想做的事 我正在尝试从任何给定的URL下载一幅图像,并从该图像中找到最常用的pixelcolor,然后将其保存为HTML颜色代码(字符串),如下所示:“#33AAFF” 这就是我目前的情况: public static string GetDominantColor(this Bitmap bmp) { //Used for tally int r = 0; int g = 0; int b = 0;

我想做的事

  • 我正在尝试从任何给定的URL下载一幅图像,并从该图像中找到最常用的pixelcolor,然后将其保存为HTML颜色代码(字符串),如下所示:“#33AAFF”
这就是我目前的情况:

public static string GetDominantColor(this Bitmap bmp)
    {
        //Used for tally
        int r = 0;
        int g = 0;
        int b = 0;

        int total = 0;

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

                r += clr.R;
                g += clr.G;
                b += clr.B;

                total++;
            }
        }

        //Calculate average
        r /= total;
        g /= total;
        b /= total;

        return ColorTranslator.ToHtml(Color.FromArgb(r, g, b));
    }
  • 我使用WebClient从URL下载图像,并将其保存为字节数组:

    byte[]data=new-WebClient()。下载数据(x.Url)

  • 我能够使用以下代码将此字节数组转换为Xamarin.Forms.ImageSource(在我的Xaml视图中显示):

    var-imageSource=imageSource.FromStream(()=>newmemoryStream(data))

我一直在尝试转换为System.Drawing.Bitmap,但遇到一些内存问题。如果我把它作为位图,那么这段代码可能会解决我的问题:

public static string GetDominantColor(this Bitmap bmp)
    {
        //Used for tally
        int r = 0;
        int g = 0;
        int b = 0;

        int total = 0;

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

                r += clr.R;
                g += clr.G;
                b += clr.B;

                total++;
            }
        }

        //Calculate average
        r /= total;
        g /= total;
        b /= total;

        return ColorTranslator.ToHtml(Color.FromArgb(r, g, b));
    }
其中,这是GetDominantColor()的方法:

public静态字符串GetDominantColor(此skbmp位图)
{
//用于理货
int r=0;
int g=0;
int b=0;
int-total=0;
对于(int x=0;x
转换为ImageSource不会帮助您进行任何分析。您也不能使用System.Drawing-而是查看SkiaSharp。您可能需要将其转换为Skia位图,并简单地遍历它并检查每个像素,除非Skia中存在某种颜色贴图,否则您可以使用哪一行导致崩溃?@Jason上次我使用此方法检查它时崩溃:
SKBitmap.Decode(downloadedBytes)
,但是现在vsstudio中的调试器不会在我的任何断点处停止。。我想我得先把它修好。谢谢你帮助我。我会让您不断更新:)您是否尝试捕获异常?好的,那么是什么异常导致了崩溃?
public static string GetDominantColor(this SKBitmap bmp)
    {
        //Used for tally
        int r = 0;
        int g = 0;
        int b = 0;

        int total = 0;

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

                r += clr.Red;
                g += clr.Green;
                b += clr.Blue;

                total++;
            }
        }

        //Calculate average
        r /= total;
        g /= total;
        b /= total;

        return $"#{r:X2}{g:X2}{b:X2}";
    }