3
Answers

change brightness of image realtime

jimmy naik

jimmy naik

14y
17.2k
1
hello guys..i want to change the brightness of the checked images that i have added in the list view in real time using trackbar i.e as i slide the trackbar i want the brightness to be changed simultaneously .can any one help me over here pls

//this is code where i loop through checked images in listview

for (int i = 0; i < listView1.Items.Count; i++)
            {
                if (listView1.Items[i].Checked)
                {
                    Bitmap img = (Bitmap)imageList.Images[i];

                    if (brighten(img, trackBar1.Value))
                    { this.Invalidate(); }
                }
            }


//this is the code to change brightness of the image

public bool brighten(Bitmap b, int trk)
        {
            if (trk < -255 || trk > 255)
                return false;
            BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            int stride = bmData.Stride;
            System.IntPtr Scan0 = bmData.Scan0;

            int nVal = 0;

            unsafe
            {
                byte* p = (byte*)(void*)Scan0;

                int nOffset = stride - b.Width * 3;
                int nWidth = b.Width * 3;

                for (int y = 0; y < b.Height; ++y)
                {
                    for (int x = 0; x < nWidth; ++x)
                    {
                        nVal = (int)(p[0] + trk);

                        if (nVal < 0) nVal = 0;
                        if (nVal > 255) nVal = 255;

                        p[0] = (byte)nVal;

                        ++p;
                    }
                    p += nOffset;
                }
            }

            b.UnlockBits(bmData);

            return true;
        }

i know this logic is wronge ..m still learning c # .pls someone help me on how to implement this properly.
i hope i was clear on my doubt.

thnks

Answers (3)