Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7928629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:47:31+00:00 2026-06-03T19:47:31+00:00

I’ve been trying to convert this JavaScript code that gets the dominant color from

  • 0

I’ve been trying to convert this JavaScript code that gets the dominant color from an image, so far with no success. I get errors with the colorCount & color variables. I don’t know the suitable & equivalent data types to use for these variables. Here is my code:

public string dominantColor(Bitmap img)
{
       int[] colorCount = new int[0];
       int maxCount = 0;
       string dominantColor = "";

       // data is an array of a series of 4 one-byte values representing the rgba values of each pixel
       Bitmap Bmp = new Bitmap(img);

       BitmapData BmpData = Bmp.LockBits(new Rectangle(0, 0, Bmp.Width, Bmp.Height), ImageLockMode.ReadOnly, Bmp.PixelFormat);
       byte[] data = new byte[BmpData.Stride * Bmp.Height];


       for (int i = 0; i < data.Length; i += 4) 
       {
            // ignore transparent pixels
            if (data[i+3] == 0)
                continue;

            string color = data[i] + "." + data[i+1] + "," + data[i+2];
            // ignore white
            if (color == "255,255,255")
               continue;

            if (colorCount[color] != 0)
               colorCount[color] = colorCount[color] + 1;
            else
               colorCount[color] = 0;

            // keep track of the color that appears the most times
            if (colorCount[color] > maxCount)
            {
                 maxCount = colorCount[color];
                 dominantColor = color.ToString;
            }
       }

       string rgb = dominantColor.Split(",");
       return rgb;   
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:47:32+00:00Added an answer on June 3, 2026 at 7:47 pm

    I’ll give you a complete managed version of your code:

    static Color dominantColor(Bitmap img)
    {
        Hashtable colorCount = new Hashtable();
        int maxCount = 0;
        Color dominantColor = Color.White;
    
        for (int i = 0; i < img.Width; i++)
        {
            for (int j = 0; j < img.Height; j++)
            {
                var color = img.GetPixel(i, j);
    
                if (color.A == 0)
                    continue;
    
                // ignore white
                if (color.Equals(Color.White))
                    continue;
    
                if (colorCount[color] != null)
                    colorCount[color] = (int)colorCount[color] + 1;
                else
                    colorCount.Add(color, 0);
    
                // keep track of the color that appears the most times
                if ((int)colorCount[color] > maxCount)
                {
                    maxCount = (int)colorCount[color];
                    dominantColor = color;
                }
            }
        }
    
        return dominantColor;
    }
    

    So what is the difference here?
    – I use a Hashtable instead of your array (you never redefine the dimension of it – and the best way to use an extensible object from JavaScript is a Hashtable)
    – I prefer to use the already included structure Color (which saves 4 bytes for Alpha, Red, Green, Blue)
    – I also do the comparisons and return this structure (then you are free to do whatever you want to do – in JavaScript using those strings is just a workaround because the browser is just giving you such RGB(a) strings)

    What is another problem in your code is the line containing byte[] data = new byte[BmpData.Stride * Bmp.Height]; – Your array is created and initialized but with no data (.NET will erase all previous data resulting in a lot of zeros). Therefore you will not anywhere.

    Drawback of my version is that it is indeed very small (this is where your lockbits are coming into play). I can give you a non-managed version (using the lockbits and an unsafe-block) if you want to. Depends if performance matters a lot for you and if you are interested!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.