C# 影像處理 顏色調整

來源:互聯網
上載者:User
/**//**//**//// <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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.