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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:08:24+00:00 2026-05-31T20:08:24+00:00

The b.GetPixel call in this method is pretty slow, is there a way to

  • 0

The b.GetPixel call in this method is pretty slow, is there a way to speed this method up with LockBits or something? But I don’t know how to work with pointers to get the pixel value etc.

Background: I need the float[][] because of weighted randomizing I asked in this question.

public static float[][] ConvertToGrayScale(this Bitmap bm)
{
    var b = new Bitmap(bm);
    var data = new List<float[]>();
    for (var i = 0; i < b.Width; i++)
    {
        var row = new List<float>();
        for (int x = 0; x < b.Height; x++)
        {
            var oc = b.GetPixel(i, x);
            var grayScale = (int)((oc.R * 0.3) + (oc.G * 0.59) + (oc.B * 0.11));
            row.Add(grayScale);
        }
        data.Add(row.ToArray());
    }
    return data.ToArray();
}

EDIT

As mentioned below from Paul Sasik I changed the code to this:

 public static GrayScaleResult ConvertToGrayScale(this Bitmap bm)
 {
     var result = new GrayScaleResult(bm);
     for (var x = 0; x < result.Width; x++)
     {
         for (int y = 0; y < result.Height; y++)
         {
             var oc = bm.GetPixel(x, y);
             // casting to int here - you can just use a 2d array of ints
             result.Data[x, y] = (int)((oc.R * 0.3) + (oc.G * 0.59) + (oc.B * 0.11));
         }
     }
     return result;
 }

 public struct GrayScaleResult
 {
     public float[,] Data;
     public int Width;
     public int Height;
     public GrayScaleResult(Bitmap bm)
     {
         Width = bm.Width;
         Height = bm.Height;
         Data = new float[Width, Height];
     }
 }

And I checked the performance with a profiler before and after that optimization:

enter image description here

It’s interesting that getHeight takes a lot of time so it seems not to be cached in the bitmap object? Because of that I stored Width and Height in the struct as well.

But the result can’t be, can it? I mean the bottleneck is still GetPixel but why is that taking more time now? I didn’t change anything else, the underlying bitmap is still the same.

EDIT2

Ok, found it: The problem was surprisingly the removal of the new Bitmap so I added it again:

 public static GrayScaleResult ConvertToGrayScale(this Bitmap b)
 {
     var bm = new Bitmap(b);
     var result = new GrayScaleResult(bm);
     ...

And now the Code is optimized:

enter image description here

  • 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-31T20:08:25+00:00Added an answer on May 31, 2026 at 8:08 pm

    Before trying unsafe code there are a number of optimizations you can make to your current code:

    public static float[,] ConvertToGrayScale2(this Bitmap bm)
    {
        var data = new float[bm.Width, bm.Height];
        for (var i = 0; i < bm.Width; i++)
        {
            for (int x = 0; x < bm.Height; x++)
            {
                var oc = bm.GetPixel(i, x);
                                // casting to int here - you can just use a 2d array of ints
                data[i, x] = (int)((oc.R * 0.3) + (oc.G * 0.59) + (oc.B * 0.11));
            }
        }
        return data;
    }
    

    Optimizations:

    • No need to create a new Bitmap to work from. Reference the one you pass in
    • Use a rectangular array of floats rather than generic Lists
    • This gets rid many extra assignment and some collection creation/management overhead
    • Please take this with a grain of salt, not using an IDE
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to call a method with argument color. But there are a lot
I know this can be done with GetPixel/SetPixel , but that takes too long.
The only way I found so far is System.Drawing.Bitmap.GetPixel() , but Microsoft has warnings
I'm attempting to call a method which is outside the class I'm working in
This sample code compares serial method with threaded method, on Quad core processor. The
I tried using Win32's GetPixel() with Ruby and it is really slow even on
In this code I would expect a call to ReleaseDC to happen in MyCanvas.Free
i have an image processing application that is able to getpixel, anyone know how
In my program this fragment: trace.log( String.Format(a= {0:R} b= {1:R} a<b= {2}, b.GetPixel(447, 517).GetBrightness(),
In my program this fragment: trace.Log( String.Format(a= {0:F10} b= {1:F10} a<b= {2}, b.GetPixel(447, 517).GetBrightness(),

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.