C# 常见图像处理效果

// 根据RGB,计算灰度值
private int GetGrayColor(System.Drawing.Color color)
{
int i = (int)(0.299 * color.R + 0.587 * color.G + 0.114 * color.B);
int i2 = (color.R * 19595 + color.G * 38469 + color.B * 7472) >> 16;
return i;
}

private void SetBitmap(Bitmap img)
{
BitmapData bmData
= img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
byte* p = (byte*)(void*)bmData.Scan0;
int offset = bmData.Stride - img.Width * 3;

for (int y = 0; y < img.Height; y++)
{
for (int x = 0; x < img.Width; x++)
{
#region 二值化

//int gray = GetGrayColor(Color.FromArgb(p[0], p[1], p[2]));
//if (gray > 128)
// p[0] = p[1] = p[2] = 255;
//else
// p[0] = p[1] = p[2] = 0;

#endregion

#region 反相

//p[0] = (byte)(255 - p[0]);
//p[1] = (byte)(255 - p[1]);
//p[2] = (byte)(255 - p[2]);

#endregion

#region 浮雕
//对图像像素点的像素值分别与相邻像素点的像素值相减后加上128, 然后将其作为新的像素点的值.

//int r = p[0] - p[0 + 3] + 128;
//int g = p[1] - p[1 + 3] + 128;
//int b = p[2] - p[2 + 3] + 128;

//if (r > 255)
// r = 255;
//else if (r < 0)
// r = 0;

//if (g > 255)
// g = 255;
//else if (g < 0)
// g = 0;

//if (b > 255)
// b = 255;
//else if (b < 0)
// b = 0;

//p[0] = (byte)r;
//p[1] = (byte)g;
//p[2] = (byte)b;

#endregion

p
+= 3;
}
p
+= offset;
}
}
img.UnlockBits(bmData);
}

  

 

posted @ 2011-09-01 19:38  一浩瀚星空一  阅读(713)  评论(0编辑  收藏  举报