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
Loading

nonePosted Feb 4, 2010, 10:34 AM
If you want the fastest methode try to change ColorMatrix
public static Bitmap AdjustBrightness(Bitmap Image, int Value)
{
System.Drawing.Bitmap TempBitmap = Image;
float FinalValue = (float)Value / 255.0f;
System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height);
System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap);
float[][] FloatColorMatrix ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {FinalValue, FinalValue, FinalValue, 1, 1}
};
System.Drawing.Imaging.ColorMatrix NewColorMatrix = newSystem.Drawing.Imaging.ColorMatrix(FloatColorMatrix);
System.Drawing.Imaging.ImageAttributes Attributes = newSystem.Drawing.Imaging.ImageAttributes();
Attributes.SetColorMatrix(NewColorMatrix);
NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, System.Drawing.GraphicsUnit.Pixel, Attributes);
Attributes.Dispose();
NewGraphics.Dispose();
return NewBitmap;
}
paul danielPosted Feb 8, 2010, 6:10 AM
http://www.codeproject.com/KB/GDI-plus/csharpgraphicfilters11.aspx
Hiren SoniPosted Feb 4, 2010, 12:16 PM
{
Bitmap bmap = b;
Color c;
for (int i = 0; i < bmap.Width; i++)
{
for (int j = 0; j < bmap.Height; j++)
{
c = bmap.GetPixel(i, j);
int cR = c.R + brightnessvalue;
int cG = c.G + brightnessvalue;
int cB = c.B + brightnessvalue;
if (cR < 0) cR = 1;
if (cR > 255) cR = 255;
if (cG < 0) cG = 1;
if (cG > 255) cG = 255;
if (cB < 0) cB = 1;
if (cB > 255) cB = 255;
bmap.SetPixel(i, j, Color.FromArgb(cR, cG, cB));
}
return bmap;
}
// here is code for brightness use as you wish in your project
// dont 4get to accept answer