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

  • SEARCH
  • Home
  • 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 270253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:58:12+00:00 2026-05-11T23:58:12+00:00

In my C# (3.5) application I need to get the average color values for

  • 0

In my C# (3.5) application I need to get the average color values for the red, green and blue channels of a bitmap. Preferably without using an external library. Can this be done? If so, how? Thanks in advance.

Trying to make things a little more precise: Each pixel in the bitmap has a certain RGB color value. I’d like to get the average RGB values for all pixels in the image.

  • 1 1 Answer
  • 1 View
  • 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-05-11T23:58:13+00:00Added an answer on May 11, 2026 at 11:58 pm

    The fastest way is by using unsafe code:

    BitmapData srcData = bm.LockBits(
                new Rectangle(0, 0, bm.Width, bm.Height), 
                ImageLockMode.ReadOnly, 
                PixelFormat.Format32bppArgb);
    
    int stride = srcData.Stride;
    
    IntPtr Scan0 = srcData.Scan0;
    
    long[] totals = new long[] {0,0,0};
    
    int width = bm.Width;
    int height = bm.Height;
    
    unsafe
    {
      byte* p = (byte*) (void*) Scan0;
    
      for (int y = 0; y < height; y++)
      {
        for (int x = 0; x < width; x++)
        {
          for (int color = 0; color < 3; color++)
          {
            int idx = (y*stride) + x*4 + color;
    
            totals[color] += p[idx];
          }
        }
      }
    }
    
    int avgB = totals[0] / (width*height);
    int avgG = totals[1] / (width*height);
    int avgR = totals[2] / (width*height);
    

    Beware: I didn’t test this code… (I may have cut some corners)

    This code also asssumes a 32 bit image. For 24-bit images. Change the x*4 to x*3

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

Sidebar

Related Questions

I need to get the spring application context from a non bean object. In
I need to get all dlls in my application root directory. What is the
I'm working on a P2P application, and I need to get it to communicate
I have an application where I get a vector<string> . I need to iterate
In my application i need to get a value from an activity to a
I'm working with C#.net developing applications for windows mobile 6, and i need get
In an application I need to execute other programs with another user's credentials. Currently
I have a java application I need to pass some info to a C++
In certain areas in my application I need data from several tables in the
I'm working on a web-application and need to figure out the best way to

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.