简体   繁体   中英

Get RGB Color Percentage From Image

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM