Page 1 of 1

Adjust Brightness with Trackbar

PostPosted: Sat Oct 08, 2011 1:53 pm
by maituanmte
Hello Everyone, I have some troubles with adjusting image's brightness. When i adjust the form like this:
the trackbar move jerkily.
My code is like that:
Code: Select all
        private int ValidateGrayLevel(int graylevel)
            if (graylevel < 0) return 0;
            if (graylevel > 255) return 255;

            return graylevel;

        private void ChangeBrightness()
            int value = this.adjustment1.Value;
            Color c;
            for (int i = 0; i < this.bitmapsource.Width; i++)
                for (int j = 0; j < this.bitmapsource.Height; j++)
                    c = this.bitmapsource.GetPixel(i, j);
                    int r = this.ValidateGrayLevel(c.R + value);
                    int g = this.ValidateGrayLevel(c.G + value);
                    int b = this.ValidateGrayLevel(c.B + value);
                    this.bitmapdisplay.SetPixel(i, j, Color.FromArgb(r, g, b));
            this.displayImage1.ImageDisplay = this.bitmapdisplay;

        private void adjustment1_ValueChanged(object sender, EventArgs e)//track bar change its value
            this.ChangeBrightness();//change brightness

so please tell me how to make it move smoothly

Re: Adjust Brightness with Trackbar

PostPosted: Sat Oct 08, 2011 3:21 pm
by andrew.kirillov

The problem is that you perform image processing every time when track bar's value is changed. This would not be too bad if the image processing routine was done better. It so happened but you implemented it in the slowest possible way. Set/GetPixel() methods are slow and don't make any good in image processing. Also using Width/Height properties in a loop is a bad decision - those properties are not done well.

If you want to make it faster, then I would suggest to learn about image processing using pointers and unsafe code blocks.
Image Processing for Dummies with C# and GDI+ Part 1 - Per Pixel Filters

P.S. What is relation of the question to IPLab application, which the topic of this board?