/**//**//**//// <summary>
/// 色彩調整
/// </summary>
/// <param name="bmp">原始圖</param>
/// <param name="rVal">r增量</param>
/// <param name="gVal">g增量</param>
/// <param name="bVal">b增量</param>
/// <returns>處理後的圖</returns>
public static Bitmap KiColorBalance(Bitmap bmp, int rVal, int gVal, int bVal)
{
if (bmp == null)
{
return null;
}
int h = bmp.Height;
int w = bmp.Width;
try
{
if (rVal > 255 || rVal < -255 || gVal > 255 || gVal < -255 || bVal > 255 || bVal < -255)
{
return null;
}
BitmapData srcData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
byte* p = (byte*)srcData.Scan0.ToPointer();
int nOffset = srcData.Stride - w * 3;
int r, g, b;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
b = p[0] + bVal;
if (bVal >= 0)
{
if (b > 255) b = 255;
}
else
{
if (b < 0) b = 0;
}
g = p[1] + gVal;
if (gVal >= 0)
{
if (g > 255) g = 255;
}
else
{
if (g < 0) g = 0;
}
r = p[2] + rVal;
if (rVal >= 0)
{
if (r > 255) r = 255;
}
else
{
if (r < 0) r = 0;
}
p[0] = (byte)b;
p[1] = (byte)g;
p[2] = (byte)r;
p += 3;
}
p += nOffset;
}
} // end of unsafe
bmp.UnlockBits(srcData);
return bmp;
}
catch
{
return null;
}
} // end of color
From:http://blog.csdn.net/ki1381/archive/2007/04/25/1584781.aspx