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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:01:28+00:00 2026-06-17T10:01:28+00:00

I am trying to filter a Bitmap image to increase or decrease Hue, Saturation,

  • 0

I am trying to filter a Bitmap image to increase or decrease Hue, Saturation, and Lightness values.

My code is working perfectly, but it is slow.

I am locking two bitmaps in memory, the original source and the current destination. The user can move various trackbar controls to modify each value which is then converted to an HSL value. For example, the values on the trackbar correspond to a range of -1.0 to 1.0.

Each time an event is thrown that the trackbar value changed, I run a function which locks the destination bitmap and applies the HSL values with the source bitmap and then stores the result in the destination bitmap. Once finished, I unlock the destination bitmap and paint the image on the screen.

Previously I used a lookup table for my other filters since I was doing per-byte operations. However I do not know how to apply that using HSL instead. Here is the code I am using:

byte red, green, blue;

for (int i = 0; i < sourceBytes.Length; i += 3)
{
    blue = sourceBytes[i];
    green = sourceBytes[i + 1];
    red = sourceBytes[i + 2];

    Color newColor = Color.FromArgb(red, green, blue);

    if (ModifyHue)
        newColor = HSL.ModifyHue(newColor, Hue);

    if (ModifySaturation)
        newColor = HSL.ModifySaturation(newColor, Saturation);

    if (ModifyLightness)
        newColor = HSL.ModifyBrightness(newColor, Lightness);

    destBytes[i] = newColor.B;
    destBytes[i + 1] = newColor.G;
    destBytes[i + 2] = newColor.R;
}

And here’s my ModifyBrightness function:

public static Color ModifyBrightness(Color color, double brightness)
{
    HSL hsl = FromRGB(color);
    hsl.L *= brightness;
    return hsl.ToRGB();
}

So basically if their brightness slider is in the very middle, its value will be 0 which I will convert to “1.0” when I pass it in to the function, so it multiplies the brightness by 1.0 which means it won’t change. If they drag the slider all the way to the right it will have a value of 100 which will result in a modifier of 2.0, so I’ll multiply the lightness value by 2.0 to double it.

  • 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-17T10:01:29+00:00Added an answer on June 17, 2026 at 10:01 am

    I ended up researching ImageAttributes and ColorMatrix and found the performance was excellent.

    Here is how I implemented it for a Saturation and Brightness filter:

    // Luminance vector for linear RGB
    const float rwgt = 0.3086f;
    const float gwgt = 0.6094f;
    const float bwgt = 0.0820f;
    
    private ImageAttributes imageAttributes = new ImageAttributes();
    private ColorMatrix colorMatrix = new ColorMatrix();
    private float saturation = 1.0f;
    private float brightness = 1.0f;
    
    protected override void OnPaint(object sender, PaintEventArgs e)
    {
        base.OnPaint(sender, e);
    
        e.Graphics.DrawImage(_bitmap, BitmapRect, BitmapRect.X, BitmapRect.Y, BitmapRect.Width, BitmapRect.Height, GraphicsUnit.Pixel, imageAttributes);
    }
    
    private void saturationTrackBar_ValueChanged(object sender, EventArgs e)
    {
        saturation = 1f - (saturationTrackBar.Value / 100f);
    
        float baseSat = 1.0f - saturation;
    
        colorMatrix[0, 0] = baseSat * rwgt + saturation;
        colorMatrix[0, 1] = baseSat * rwgt;
        colorMatrix[0, 2] = baseSat * rwgt;
        colorMatrix[1, 0] = baseSat * gwgt;
        colorMatrix[1, 1] = baseSat * gwgt + saturation;
        colorMatrix[1, 2] = baseSat * gwgt;
        colorMatrix[2, 0] = baseSat * bwgt;
        colorMatrix[2, 1] = baseSat * bwgt;
        colorMatrix[2, 2] = baseSat * bwgt + saturation;
    
        imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
    
        Invalidate();
    }
    
    private void brightnessTrackBar_ValueChanged(object sender, EventArgs e)
    {
        brightness = 1f + (brightnessTrackBar.Value / 100f);
    
        float adjustedBrightness = brightness - 1f;
    
        colorMatrix[4, 0] = adjustedBrightness;
        colorMatrix[4, 1] = adjustedBrightness;
        colorMatrix[4, 2] = adjustedBrightness;
    
        imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
    
        Invalidate();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to filter through some images. Each image has 3 sets of values
I'm trying to filter a datatable with following code private void Filter(string text) {
I'm trying to filter by a list of values using the criteria API. I
I'm trying to resize the image after upload but it doesn't work What's wrong?
I'm trying to filter ALL widget output through a simple filter, but can't find
Interesting question for someone. I'm trying to apply an SVG filter to an image
I am trying to filter an image with out using imfilter . I should
I am trying to implement this code: package fortyonepost.com.iapa; import android.app.Activity; import android.graphics.Bitmap; import
I'm trying to place a background in my activity, but the image quality is
I'm trying to filter my list of auditions by show ID but when 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.