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

In my Windows Phone application I need to get hash using SHA-1 Algorithm. How
I'm using SQLite database in my application and i need to get data from
In my application i need to get some data from server using http. The
In my ASP.net MVC application i need to get the host of the application
Just need get some vals located in application.ini(main ini) in the Controller plugin I
I am developing a Facebook Application that I need to get all the information
I need to get an instance of the application's MembershipProvider from within a controller.
I need to get the path (not the executable) where my application is running
I need to get the Handler to the child Window of a certain application
iam developing one application.In that i need to get the music files from the

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.