从图像中获取RGB颜色百分比

时间:2013-08-19 19:07:56

标签: c# bitmap microsoft-metro windows-store-apps writablebitmap

如何使用可写位图从Windows应用商店中的图像中获取RGB颜色百分比。

在Windows应用程序中,我是这样得到的:

public static Color getDominantColor(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 Color.FromArgb(r, g, b);
}

如何使用Metro App中的可写位图执行此操作?

1 个答案:

答案 0 :(得分:1)

查看BitmapDecoder课程。它应该拥有你需要的一切。

基本上,异步获取像素数据,然后按原样使用它。请注意,有时像素数据以16位块(每个两个字节)保存,因此您必须对字节数组执行快速Linq调用,以将其转换为可用的像素阵列。

相关问题