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 5995099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:54:43+00:00 2026-05-22T23:54:43+00:00

I need to do some thresholding for my image. The threshold filter function just

  • 0

I need to do some thresholding for my image. The threshold filter function just accepts 8-16bpp grayscale. My bitmap picture has the 32bppRGB pixelformat. Please suggest some code for the same. (I also want to know if it is possible without pixel by pixel operations)

p.s. I am using the Aforge.NET for thresholding.

Thanks

-Sagar

  • 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-05-22T23:54:44+00:00Added an answer on May 22, 2026 at 11:54 pm

    Easiest way:

    public static Bitmap MakeGrayscale(Bitmap original)
     {
        //make an empty bitmap the same size as original
        Bitmap newBitmap = new Bitmap(original.Width, original.Height);
    
       for (int i = 0; i < original.Width; i++)
        {
           for (int j = 0; j < original.Height; j++)
           {
              //get the pixel from the original image
              Color originalColor = original.GetPixel(i, j);
    
             //create the grayscale version of the pixel
              int grayScale = (int)((originalColor.R * .3) + (originalColor.G * .59)
                  + (originalColor.B * .11));
    
             //create the color object
              Color newColor =  Color.FromArgb(grayScale, grayScale, grayScale);
    
              //set the new image's pixel to the grayscale version
              newBitmap.SetPixel(i, j, newColor);
             }
         }
    
        return newBitmap;
     }
    

    Faster way:

    public static Bitmap MakeGrayscale2(Bitmap original)
     {
        unsafe
        {
           //create an empty bitmap the same size as original
           Bitmap newBitmap = new Bitmap(original.Width, original.Height);
    
          //lock the original bitmap in memory
           BitmapData originalData = original.LockBits(
              new Rectangle(0, 0, original.Width, original.Height),
              ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
    
          //lock the new bitmap in memory
           BitmapData newData = newBitmap.LockBits(
              new Rectangle(0, 0, original.Width, original.Height), 
             ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
    
           //set the number of bytes per pixel
           int pixelSize = 3;
    
          for (int y = 0; y < original.Height; y++)
           {
              //get the data from the original image
              byte* oRow = (byte*)originalData.Scan0 + (y * originalData.Stride);
    
             //get the data from the new image
              byte* nRow = (byte*)newData.Scan0 + (y * newData.Stride);
    
             for (int x = 0; x < original.Width; x++)
              {
                 //create the grayscale version
                 byte grayScale = 
                   (byte)((oRow[x * pixelSize] * .11) + //B
                    (oRow[x * pixelSize + 1] * .59) +  //G
                    (oRow[x * pixelSize + 2] * .3)); //R
    
                //set the new image's pixel to the grayscale version
                 nRow[x * pixelSize] = grayScale; //B
                 nRow[x * pixelSize + 1] = grayScale; //G
                 nRow[x * pixelSize + 2] = grayScale; //R
              }
           }
    
          //unlock the bitmaps
           newBitmap.UnlockBits(newData);
           original.UnlockBits(originalData);
    
          return newBitmap;
        }
     }
    

    Fastest way:

    public static Bitmap MakeGrayscale3(Bitmap original)
     {
        //create a blank bitmap the same size as original
        Bitmap newBitmap = new Bitmap(original.Width, original.Height);
    
        //get a graphics object from the new image
        Graphics g = Graphics.FromImage(newBitmap);
    
       //create the grayscale ColorMatrix
        ColorMatrix colorMatrix = new ColorMatrix(
           new float[][] 
          {
              new float[] {.3f, .3f, .3f, 0, 0},
              new float[] {.59f, .59f, .59f, 0, 0},
              new float[] {.11f, .11f, .11f, 0, 0},
              new float[] {0, 0, 0, 1, 0},
              new float[] {0, 0, 0, 0, 1}
           });
    
       //create some image attributes
        ImageAttributes attributes = new ImageAttributes();
    
       //set the color matrix attribute
        attributes.SetColorMatrix(colorMatrix);
    
       //draw the original image on the new image
        //using the grayscale color matrix
        g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
           0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
    
       //dispose the Graphics object
        g.Dispose();
        return newBitmap;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some help, I have a regular expression that appears to work just fine
Need some Advice on how to reference elements with jQuery (DOM/Traversing/Selectors). Sometimes its just
Need some pointers to resources for learning about Image processing used in Augmented Reality.
Need some help refactoring this code: $(span[rel=color_content]).ColorPicker({ onChange: function (hsb, hex, rg) { $(span[rel=color_content]).css('background-color',
I need some advice as to how I easily can separate test runs for
I need some info on how to use margins and how exactly padding works.
I need some software to explore and modify some SQLite databases. Does anything similar
I need some help from the shell-script gurus out there. I have a .txt
We need some input on what is a good design pattern on using AJAX
I need some sort of interactive chart control for my .NET-based web app. I

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.