C# Programming
06-10-2009, 08:22 PM
Hi Everyone
please look at following two line this is not working when i execute my prject
int div = Convert.ToInt32(textBox1.Text.Trim());
ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / div);
but if i remove the first line and enter an integer value in place of div variable it works such as following line.
ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / 3);
Following is the full listing may it help you to figurout the problem
a method that convert a bitmap from colored to grayscale
//////////////////////////////////////////////////////////////////////////
public Bitmap GrayScale(Bitmap bmpimg)
{
BitmapData bmpData = bmpimg.LockBits(new Rectangle(0, 0, bmpimg.Width, bmpimg.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int remain = bmpData.Stride - bmpData.Width * 3;
unsafe
{
byte* ptr = (byte*)bmpData.Scan0;
for (int i = 0; i < bmpData.Height; i++)
{
for (int j = 0; j < bmpData.Width; j++)
{
int div = Convert.ToInt32(textBox1.Text.Trim());
ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / div);
ptr += 3;
}
ptr += remain;
}
}
bmpimg.UnlockBits(bmpData);
return bmpimg;
}
please look at following two line this is not working when i execute my prject
int div = Convert.ToInt32(textBox1.Text.Trim());
ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / div);
but if i remove the first line and enter an integer value in place of div variable it works such as following line.
ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / 3);
Following is the full listing may it help you to figurout the problem
a method that convert a bitmap from colored to grayscale
//////////////////////////////////////////////////////////////////////////
public Bitmap GrayScale(Bitmap bmpimg)
{
BitmapData bmpData = bmpimg.LockBits(new Rectangle(0, 0, bmpimg.Width, bmpimg.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int remain = bmpData.Stride - bmpData.Width * 3;
unsafe
{
byte* ptr = (byte*)bmpData.Scan0;
for (int i = 0; i < bmpData.Height; i++)
{
for (int j = 0; j < bmpData.Width; j++)
{
int div = Convert.ToInt32(textBox1.Text.Trim());
ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / div);
ptr += 3;
}
ptr += remain;
}
}
bmpimg.UnlockBits(bmpData);
return bmpimg;
}