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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:35:28+00:00 2026-06-12T17:35:28+00:00

My Application I am writing an application that needs to convert RGB to grayscale

  • 0

My Application

I am writing an application that needs to convert RGB to grayscale images.
The conversion works but converting an image of 3648 * 2736 pixel takes round about 7 secs.
I know that set and getpixel take some time.
But I think that it shouldn’t take so long if you are using Lockbits even though the image is not small. (please correct me if that is wrong).
Maybe I just did a fatal mistake within my code.

The code

public static long ConvertToGrayScaleV2(Bitmap imageColor, bool useHDTVConversion)
        {
            Stopwatch stpw = new Stopwatch();
            stpw.Start();
            System.Drawing.Imaging.BitmapData imageColorData = imageColor.LockBits(new Rectangle(new Point(0, 0), imageColor.Size),
                System.Drawing.Imaging.ImageLockMode.ReadWrite, imageColor.PixelFormat);

            IntPtr PtrColor = imageColorData.Scan0;
            int strideColor = imageColorData.Stride;
            byte[] byteImageColor = new byte[Math.Abs(strideColor) * imageColor.Height];
            System.Runtime.InteropServices.Marshal.Copy(PtrColor, byteImageColor, 0, Math.Abs(strideColor) * imageColor.Height);

            int bytesPerPixel = getBytesPerPixel(imageColor);
            byte value;
            if (bytesPerPixel == -1)
                throw new Exception("Can't get bytes per pixel because it is not defined for this image format.");
            for (int x = 0, position; x < imageColor.Width * imageColor.Height; x++)
            {
                position = x * bytesPerPixel;
                if (useHDTVConversion)
                {
                    value = (byte)(byteImageColor[position] * 0.0722 + byteImageColor[position + 1] * 0.7152 + byteImageColor[position + 2] * 0.2126);                    
                }
                else
                {
                    value = (byte)(byteImageColor[position] * 0.114 + byteImageColor[position + 1] * 0.587 + byteImageColor[position + 2] * 0.299);
                }
                byteImageColor[position] = value;
                byteImageColor[position+1] = value;
                byteImageColor[position+2] = value;

            }

            System.Runtime.InteropServices.Marshal.Copy(byteImageColor, 0, PtrColor, Math.Abs(strideColor) * imageColor.Height);
            imageColor.UnlockBits(imageColorData);
            stpw.Stop();
            return stpw.ElapsedMilliseconds;
        }

        public static int getBytesPerPixel(Image img)
        {
            switch (img.PixelFormat)
            {
                case System.Drawing.Imaging.PixelFormat.Format16bppArgb1555:    return 2;
                case System.Drawing.Imaging.PixelFormat.Format16bppGrayScale:   return 2;
                case System.Drawing.Imaging.PixelFormat.Format16bppRgb555: return 2;
                case System.Drawing.Imaging.PixelFormat.Format16bppRgb565: return 2;
                case System.Drawing.Imaging.PixelFormat.Format1bppIndexed: return 1;
                case System.Drawing.Imaging.PixelFormat.Format24bppRgb: return 3;
                case System.Drawing.Imaging.PixelFormat.Format32bppArgb: return 4;
                case System.Drawing.Imaging.PixelFormat.Format32bppPArgb: return 4;
                case System.Drawing.Imaging.PixelFormat.Format32bppRgb: return 4;
                case System.Drawing.Imaging.PixelFormat.Format48bppRgb: return 6;
                case System.Drawing.Imaging.PixelFormat.Format4bppIndexed: return 1;
                case System.Drawing.Imaging.PixelFormat.Format64bppArgb: return 8;
                case System.Drawing.Imaging.PixelFormat.Format64bppPArgb: return 8;
                case System.Drawing.Imaging.PixelFormat.Format8bppIndexed: return 1;
                default: return -1;
            }

        }
  • 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-12T17:35:29+00:00Added an answer on June 12, 2026 at 5:35 pm

    If you’re converting to greyscale, try using a ColorMatrix transformation instead.
    from: https://web.archive.org/web/20141230145627/http://bobpowell.net/grayscale.aspx

       Image img = Image.FromFile(dlg.FileName);
            Bitmap bm = new Bitmap(img.Width,img.Height);
            Graphics g = Graphics.FromImage(bm); 
    
      
    
            
            ColorMatrix cm = new ColorMatrix(new float[][]{   new float[]{0.5f,0.5f,0.5f,0,0},
                                      new float[]{0.5f,0.5f,0.5f,0,0},
                                      new float[]{0.5f,0.5f,0.5f,0,0},
                                      new float[]{0,0,0,1,0,0},
                                      new float[]{0,0,0,0,1,0},
                                      new float[]{0,0,0,0,0,1}}); 
    
            
    
            /* 
    
            //Gilles Khouzams colour corrected grayscale shear
            ColorMatrix cm = new ColorMatrix(new float[][]{   new float[]{0.3f,0.3f,0.3f,0,0},
                                      new float[]{0.59f,0.59f,0.59f,0,0},
                                      new float[]{0.11f,0.11f,0.11f,0,0},
                                      new float[]{0,0,0,1,0,0},
                                      new float[]{0,0,0,0,1,0},
                                      new float[]{0,0,0,0,0,1}}); 
    
            */ 
    
    
            ImageAttributes ia = new ImageAttributes();
            ia.SetColorMatrix(cm);
            g.DrawImage(img,new Rectangle(0,0,img.Width,img.Height),0,0,img.Width,img.Height,GraphicsUnit.Pixel,ia);
            g.Dispose();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an application that needs to uncompress data compressed by another application (which
I'm writing an application in C# that needs to decrypt some data that was
I am writing an iPhone application that needs to record audio from the built-in
I am writing a Facebook application that needs to post on a friend's wall
I'm writing a windows service application that needs to serialize and deserialize XML documents
I am writing a .NET WinForms application that needs to display a list of
I am writing an Adobe AIR application that needs to build in a CI
I am writing a java application that takes schema-bounded XML as input and needs
I am developing an application that displays 16 bit grayscale images. The UI for
I am writing an application that needs to create an object relative to 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.