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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:17:41+00:00 2026-05-15T06:17:41+00:00

I am changing color values of each pixel in an image based on a

  • 0

I am changing color values of each pixel in an image based on a calculation. The problem is that this takes over 5 seconds on my machine with a 1000×1333 image and I’m looking for a way to optimize it to be much faster.

I think ColorMatrix may be an option, but I’m having a difficult time figure out how I would get a set of pixel RGB values, use that to calculate and then set the new pixel value. I can see how this can be done if I was just modifying (multiplying, subtracting, etc.) the original value with ColorMatrix, but now how I can use the pixels returned value to use it to calculate and new value.

For example:

Sub DarkenPicture()
    Dim clrTestFolderPath = "C:\Users\Me\Desktop\ColorTest\"
    Dim originalPicture = "original.jpg"
    Dim Luminance As Single
    Dim bitmapOriginal As Bitmap = Image.FromFile(clrTestFolderPath + originalPicture)
    Dim Clr As Color
    Dim newR As Byte
    Dim newG As Byte
    Dim newB As Byte
    For x = 0 To bitmapOriginal.Width - 1
        For y = 0 To bitmapOriginal.Height - 1
            Clr = bitmapOriginal.GetPixel(x, y)
            Luminance = ((0.21 * (Clr.R) + (0.72 * (Clr.G)) + (0.07 * (Clr.B))/ 255
            newR = Clr.R * Luminance
            newG = Clr.G * Luminance
            newB = Clr.B * Luminance
            bitmapOriginal.SetPixel(x, y, Color.FromArgb(newR, newG, newB))
        Next
    Next
    bitmapOriginal.Save(clrTestFolderPath + "colorized.jpg", ImageFormat.Jpeg)
End Sub

The Luminance value is the calculated one. I know I can set ColorMatrix‘s M00, M11, M22 to 0, 0, 0 respectively and then put a new value in M40, M41, M42, but that new value is calculated based of a value multiplication and addition of that pixel’s components (((0.21 * (Clr.R) + (0.72 * (Clr.G)) + (0.07 * (Clr.B)) and the result of that – Luminance – is multiplied by the color component).

Is this even possible with ColorMatrix?

  • 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-15T06:17:41+00:00Added an answer on May 15, 2026 at 6:17 am

    No. Making any one of the color components being multiplied with itself or any other component is not possible with a ColorMatrix. You can ony multiply the components with constants and then add the parts together.

    But what you can do is to write this into C# and use .LockBits instead of GetPixel/SetPixel. That would be even faster that what you could possibly achieve with a ColorMatrix.

    UPDATE: some sample code:

        private static void myVerySpecialSepia(
            IntPtr source,
            IntPtr destination,
            int height,
            int width,
            int sourceStride,
            int destinationStride,
            int sourceBytesPerPixel,
            int destinationBytesPerPixel )
        {
            unsafe
            {
                for ( int y = 0 ; y < height ; y++ )
                {
                    byte* pOrig = (byte*)source.ToPointer() + sourceStride * y;
                    byte* pDest = (byte*)destination.ToPointer() + destinationStride * y;
                    for ( int x = width ; x > 0 ; x-- )
                    {
                        float b = pOrig[0];
                        float g = pOrig[1];
                        float r = pOrig[2];
                        float b2 = b * b;
                        float g2 = g * g;
                        float r2 = r * r;
                        pDest[0] = (byte)(
                            b * 0.400367618f + b2 * 0.00011502471f +
                            g * (-0.0337239578f) + g2 * 0.00056673412f +
                            r * 0.221445322f + r2 * 0.0008506606f +
                            6.2766808485f);
                        pDest[1] = (byte)(
                            b * 0.493460029f + b2 * (-0.00023297003f) +
                            g * (-0.008577178f) + g2 * 0.00031247039f +
                            r * 0.5043012 + r2 * (-0.00006892065f) +
                            0.2746957206f);
                        pDest[2] = (byte)(
                            b * 0.617727f + b2 * (-0.00070876251f) +
                            g * 0.00271902746f + g2 * 0.00007401942f +
                            r * 0.6954346f + r2 * (-0.00065937551f) +
                            0.116103285f);
                        pOrig += sourceBytesPerPixel;
                        pDest += destinationBytesPerPixel;
                    }
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This concerns iPhoneOS-sdk-3.2 I am having difficulty changing the border color of a grouped
I want to generate tooltip based on a dynamically changing background image in css.
I have this select that is constantly changing (adding, removing, etc)... It goes something
I have this piece of javascript that is working. var footerColour = $.color.extract($(div#one.footer), 'background-color');
This should be simple but I'm having trouble changing some C code that uses
I am having trouble changing the color of a menu item (.category) on hover
I am changing the background color of the cells when the user has made
I'm having some trouble changing the colour and font on a TableView that I
changing a static resource during runtine is something that sounds not possible. I have
I'm working on a project that requires me to select unique colors for each

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.