Get RGB Color Percentage From Image

1.3k views Asked by At

How does one get the RGB color percentages from an image in windows app store by using a writable bitmap.

In Windows app I was getting it earlier like this:

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);
}

How can you do this with a writable bitmap in a Metro App?

1

There are 1 answers

0
Nate Diamond On

Check out the BitmapDecoder class. It should have everything you need.

Basically, get the pixel data asynchronously, then use it as you have. Just be warned that sometimes the pixel data is saved in 16-bit chunks (two bytes each), so you'll have to do a quick Linq call on the byte array to turn it into a usable pixel array.